llputney
asked on
Update with null instead of empty string
I have a form that one can go to and edit information. If a user does not enter info on a field, it will show as an empty string in the database. How can I get it to update with a Null value instead of an empty string? However, there are certain fields that will update with a null value instead of a string. See code:
Here is a portion of my ASP code:
if request("formcode") = "update" then
if request("txtApplication") = "ON" then strApplication = 1 else strApplication = 0
if request("txtEligible") = "ON" then strEligible = 1 else strEligible = 0
if request("txtDelete") = "ON" then strActive = 0 else strActive = 1
if isDate(request("txtDateDis continued" )) then strDateDiscontinued = request("txtDateDiscontinu ed") else strDateDiscontinued = "Null"
if isDate(request("txtDateEli gible")) then strDateEligible = request("txtDateEligible") else strDateEligible = "Null"
if isDate(request("txtDateOfB irth")) then strDateOfBirth = request("txtDateOfBirth") else strDateOfBirth = "Null"
strSQL = "UPDATE tedp_consumer SET " & _
"staffid = " & escapeSQL(request("txtStaf fID")) & ", " & _
"active = " & escapeSQL(stractive) & ", " & _
"First_Name = " & escapeSQL(request("txtFirs tName")) & ", " & _
"Last_Name = " & escapeSQL(request("txtLast Name")) & ", " & _
"Address = " & escapeSQL(request("txtAddr ess")) & ", " & _
"City = " & escapeSQL(request("txtCity ")) & ", " & _
"State = " & escapeSQL(request("txtStat e")) & ", " & _
"Zipcode = " & escapeSQL(request("txtZipc ode")) & ", " & _
"County = " & escapeSQL(request("txtCoun ty")) & ", " & _
"SSN = " & escapeSQL(request("txtSSN" )) & ", " & _
"Home_Phone = " & escapeSQL(request("txtHome Phone")) & ", " & _
"Work_Phone = " & escapeSQL(request("txtWork Phone")) & ", " & _
"Consumer_Type = " & escapeSQL(request("txtCons umerType") ) & ", " & _
"Status = " & escapeSQL(request("txtStat us")) & ", " & _
"Eligible = " & escapeSQL(strEligible) & ", " & _
"Date_Eligible = " & escapeSQL(strDateEligible) & ", " & _
"date_discontinued = " & escapeSQL(strDateDiscontin ued) & ", " & _
"reason_discontinued = " & escapeSQL(request("txtReas onDisconti nued")) & ", " & _
"DateOfBirth = " & escapeSQL(strDateOfBirth) & ", " & _
"Gender = " & escapeSQL(request("txtGend er")) & ", " & _
"Ethnic = " & escapeSQL(request("txtEthn ic")) & ", " & _
"OfficeID = " & escapeSQL(request("txtOffi ce")) & ", " & _
"VerName = " & escapeSQL(request("txtVerN ame")) & ", " & _
"Veraddress = " & escapeSQL(request("txtVera ddress")) & ", " & _
"Verphone = " & escapeSQL(request("txtVerp hone")) & ", " & _
"Veroccupation = " & escapeSQL(request("txtVero ccupation" )) & ", " & _
"Case_Notes = " & escapeSQL(request("txtCase Notes")) & ", " & _
"Application_Complete = " & escapeSQL(strApplication) & " " & _
"WHERE id = " & escapeSQL(strID)
oConn.execute strSQL
Here is a portion of my ASP code:
if request("formcode") = "update" then
if request("txtApplication") = "ON" then strApplication = 1 else strApplication = 0
if request("txtEligible") = "ON" then strEligible = 1 else strEligible = 0
if request("txtDelete") = "ON" then strActive = 0 else strActive = 1
if isDate(request("txtDateDis
if isDate(request("txtDateEli
if isDate(request("txtDateOfB
strSQL = "UPDATE tedp_consumer SET " & _
"staffid = " & escapeSQL(request("txtStaf
"active = " & escapeSQL(stractive) & ", " & _
"First_Name = " & escapeSQL(request("txtFirs
"Last_Name = " & escapeSQL(request("txtLast
"Address = " & escapeSQL(request("txtAddr
"City = " & escapeSQL(request("txtCity
"State = " & escapeSQL(request("txtStat
"Zipcode = " & escapeSQL(request("txtZipc
"County = " & escapeSQL(request("txtCoun
"SSN = " & escapeSQL(request("txtSSN"
"Home_Phone = " & escapeSQL(request("txtHome
"Work_Phone = " & escapeSQL(request("txtWork
"Consumer_Type = " & escapeSQL(request("txtCons
"Status = " & escapeSQL(request("txtStat
"Eligible = " & escapeSQL(strEligible) & ", " & _
"Date_Eligible = " & escapeSQL(strDateEligible)
"date_discontinued = " & escapeSQL(strDateDiscontin
"reason_discontinued = " & escapeSQL(request("txtReas
"DateOfBirth = " & escapeSQL(strDateOfBirth) & ", " & _
"Gender = " & escapeSQL(request("txtGend
"Ethnic = " & escapeSQL(request("txtEthn
"OfficeID = " & escapeSQL(request("txtOffi
"VerName = " & escapeSQL(request("txtVerN
"Veraddress = " & escapeSQL(request("txtVera
"Verphone = " & escapeSQL(request("txtVerp
"Veroccupation = " & escapeSQL(request("txtVero
"Case_Notes = " & escapeSQL(request("txtCase
"Application_Complete = " & escapeSQL(strApplication) & " " & _
"WHERE id = " & escapeSQL(strID)
oConn.execute strSQL
You need to set the value to NULL in your escapeSQL() itlooks like. In SQL single quotes determine if empty string is entered or not, but I don't see any in your code. So, I guess you do that in escapeSQL function
Write a general function like this one (I'm not entirely sure if that's the correct way to escape quotes, didn't have anything to test this on):
Function empty2null(s)
If (s = """""")
empty2null = "NULL"
Else
empty2null = s
End If
End Function
... and instead of ...
escapeSQL(request("txtVerN ame"))
...you should use...
empty2null(escapeSQL(reque st("txtVer Name")))
...for the fields where you wish to have NULL instead of empty string.
Function empty2null(s)
If (s = """""")
empty2null = "NULL"
Else
empty2null = s
End If
End Function
... and instead of ...
escapeSQL(request("txtVerN
...you should use...
empty2null(escapeSQL(reque
...for the fields where you wish to have NULL instead of empty string.
ASKER
I found the code of the escapeSQL function
' escapeSQL prepares variables for SQL statements
' It just strips out nulls, escapes any single-quotes inside the string,
' and wraps strings with single-quotes. Any variable passed to SQL is
' run through this function.
function escapeSQL(strInput)
strOutput = strInput
If isNull(strOutput) then strOutput = ""
strOutput = replace(strOutput,"'","''" )
if Not isNumeric(strOutput) then strOutput = "'" & strOutput & "'"
if isNumeric(strOutput) AND instr(strOutput,"-") > 0 then strOutput = "'" & strOutput & "'"
If strInput = "Null" then strOutput = "Null"
escapeSQL = strOutput
end function
Im not sure why this line "If isNull(strOutput) then strOutput = """ is there, but I tried removing it and updated the form with some empty fields, it still didin't give me a NULL value.
' escapeSQL prepares variables for SQL statements
' It just strips out nulls, escapes any single-quotes inside the string,
' and wraps strings with single-quotes. Any variable passed to SQL is
' run through this function.
function escapeSQL(strInput)
strOutput = strInput
If isNull(strOutput) then strOutput = ""
strOutput = replace(strOutput,"'","''"
if Not isNumeric(strOutput) then strOutput = "'" & strOutput & "'"
if isNumeric(strOutput) AND instr(strOutput,"-") > 0 then strOutput = "'" & strOutput & "'"
If strInput = "Null" then strOutput = "Null"
escapeSQL = strOutput
end function
Im not sure why this line "If isNull(strOutput) then strOutput = """ is there, but I tried removing it and updated the form with some empty fields, it still didin't give me a NULL value.
Try to this:
function escapeSQL(strInput)
strOutput = strInput
If isNull(strOutput) then
strOutput = NULL
exit function
end if
strOutput = replace(strOutput,"'","''" )
if Not isNumeric(strOutput) then strOutput = "'" & strOutput & "'"
if isNumeric(strOutput) AND instr(strOutput,"-") > 0 then strOutput = "'" & strOutput & "'"
If strInput = "Null" then strOutput = "Null"
escapeSQL = strOutput
end function
function escapeSQL(strInput)
strOutput = strInput
If isNull(strOutput) then
strOutput = NULL
exit function
end if
strOutput = replace(strOutput,"'","''"
if Not isNumeric(strOutput) then strOutput = "'" & strOutput & "'"
if isNumeric(strOutput) AND instr(strOutput,"-") > 0 then strOutput = "'" & strOutput & "'"
If strInput = "Null" then strOutput = "Null"
escapeSQL = strOutput
end function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
golfDoctor: your function didn't work :-(
still need help? if not, please close this question.
the function works
the function works
"date_discontinued = " & escapeSQL(strDateDiscontin
which is probably putting quotes around the value.
and THAT is the problem, in case the values passed is "NULL".
you could change the function escapeSQL to NOT put quotes wheneven the string value is "NULL"...
however, the preferred method would be to make a parametrized query.
that would take away from you all the issues of the quoting...