Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How use NULL in SQL INSERT - VB6

Below is an INSERT I use in VB6 for SQL Server. The last variable I sometimes pass as a NULL. When I remove the "" from around the variable the program will not execute. When I place the "" around it as in the example the resulting value is 0 (zero) as the field is an INT (and it allows NULLs). How can I write this so a NULL is valid in the INSERT?
Thanks

cnn.Execute "INSERT INTO SLSalesOrder (" & _
"SLSO_CorporateId, SLSO_CompanyId, SLSO_DivisionId, " & _
"SLSO_Number, SLSO_Date, SLSO_Status, " & _
"SLSO_Type, " & _
"SLSO_Cust_Id, SLSO_Cust_ShipToId, " & _
"SLSO_Instructions, " & _
"SLSO_LineItemCount, " & _
"SLSO_FleetCount, " & _
"SLSO_AssignedTruckId, SLSO_AssignedDate, SLSO_AssignedShift " & _
") " & _
"VALUES " & _
"(" & _
"'" & SavCustomerCorp & "', '000', '000', " & _
"'" & SavSalesOrderNumber & "', '" & SavSalesOrderDateTime & "', '" & SavStatus & "', " & _
"'" & SavCustomerTickType & "', " & _
"'" & SavSLCust_ShipToCustomerId & "', '" & SavSLCust_ShipToId & "', " & _
"'" & SavOrderInstructions & "', " & _
ETicketProductPointer & ", " & _
"'" & SavFleetVehicleCount & "', " & _
"'" & SavAssignedTruckId & "', '" & SavAssignedDate & "', '" & SavAssignedShift & "'" & _
");"
[1797 byte] By [Don] at [2007-11-11 7:13:29]
# 1 Re: How use NULL in SQL INSERT - VB6
Try this:

'" & SavAssignedDate & "', '" & _
IIf(IsNull(SavAssignedShift), "null", "'" & SavAssignedShift & "'") & _
");"
Phil Weber at 2007-11-11 17:27:14 >
# 2 Re: How use NULL in SQL INSERT - VB6
Thanks...Did not think about that. Works just like I need it too!
Don at 2007-11-11 17:28:15 >