WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

SQL String Handler - updated 6-Feb-2003

Summary: When you build a SQL string, you need to take apostrophes, dates and when updating data NULL values need to be accounted for.
 
Keywords: APOSTROPHE
DATE
SQL
Applicable Software: Active Server Pages (ASP)
ADO
Microsoft Access
Microsoft SQL Server
 
Body:

This is an updated version of this article. I have added support for comma-delimited lists (to support querystrings and form collections).

Note: this function requires the FixNumber Function.

Note: there is now a .NET version of this function available.

Const sqlDataTypeText = 0
Const sqlDataTypeNumber = 1
Const sqlDataTypeDate = 2
Const sqlDataTypeCommaDelimNumber = 3
Const sqlDataTypeCommaDelimText = 4

Function SQLStringFieldValue(fieldValue, dataType)
Dim m_sOut, m_sTmp
Dim m_iCnt, m_iTmp
Dim m_aTmp

	If Len(Trim(fieldValue)) > 0 Then
		Select Case dataType
			Case sqlDataTypeText
				m_sOut = "'" & Replace(Trim(fieldValue), "'", "''") & "'"

			Case sqlDataTypeNumber
				m_sOut = FixNumber(fieldValue)

			Case sqlDataTypeDate
				If IsDate(fieldValue) Then
					m_sOut = "#" & Trim(fieldValue) & "#"

				End If

			Case sqlDataTypeCommaDelimNumber
				m_aTmp = Split(fieldValue, ",")
				For m_iCnt = 0 To UBound(m_aTmp)
					m_iTmp = FixNumber(m_aTmp(m_iCnt))
					If m_iTmp > 0 Then
						If Len(m_sOut) > 0 Then
							m_sOut = m_sOut & ","

						End If

						m_sOut = m_sOut & m_iTmp

					End If

				Next

			Case sqlDataTypeCommaDelimText
				m_aTmp = Split(fieldValue, ",")
				For m_iCnt = 0 To UBound(m_aTmp)
					m_sTmp = Trim(m_aTmp(m_iCnt) & "")
					If Len(m_sTmp) > 0 Then
						If Len(m_sOut) > 0 Then
							m_sOut = m_sOut & ","

						End If

						m_sOut = m_sOut & "'" & Replace(m_sTmp, "'", "''") & "'"

					End If

				Next

		End Select

	End If


	If Len(m_sOut) = 0 Then
		m_sOut = "NULL"

	End If

	SQLStringFieldValue = m_sOut

End Function

To use this, just call the function where you would normally assign a value:

SQL = "SELECT * " & _
    "FROM Table " & _
    "WHERE LastName = " & SQLStringFieldValue(Request.Form("LastName"), sqlDataTypeText)

or...

SQL = "UPDATE TableName " & _
      "SET " & _
      "Age = " & SQLStringFieldValue(AGE, sqlDataTypeNumber) & " " & _
      "WHERE BirthDate >= " & SQLStringFieldValue(BIRTH_DATE, sqlDataTypeDate) & " "

Please note: this function needs to be modified for SQL Server. MS Access uses the # character around dates; SQL Server use the ' character.

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 2/6/2003 7:55:34 AM

Rate this article: Average: 10
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.