Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting blank fields to be NULL

Status
Not open for further replies.

Rexolio

Technical User
Aug 29, 2001
230
when someone fills out my form and leaves an unimportant field blank (I have a validation script running for the important ones), the field in my SQL database doesn't automatically go to NULL. I use TRIM(Request.form("")), but this doesn't work. I have many fields and although I know I could do an if/then statement for each, making each blank value a NULL value, I'd rather not too that if I don't have too.
 
You need to write a function to which you pass individual field values, and the function converts the value to a null if the user did not eneterd a field. If you are using SQL 2000 you can write a user defined function which will return NULL. otherwise you can write it in VB or ASP.
 
You can use NULLIF function provided by SQL, which takes 2 parameters. If both parameters are same it returns NULL and it returns the first parameter if the 2 are not same.

In your case you need to do this for each parameter

INSERT INTO ...
VALUES (NULLIF(@param1,''),NULLIF(@param2,'')....)

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top