Link to home
Start Free TrialLog in
Avatar of MFredin
MFredinFlag for United States of America

asked on

Need to insert null into date field

I am using MySql 5 and Coldfusion 8 to insert some dates into a table.  I want to be able to insert a null value into a date field if that is possible. Basically, if the field is blank, do not insert anything to the date field.  When I try to insert nothing into the field I get the following error...

Data truncation: Incorrect date value: '' for column 'my_date_field' at row 1

Is there anyway to do this?
ASKER CERTIFIED SOLUTION
Avatar of Raynard7
Raynard7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of js_vaughan
js_vaughan

There was a topic on this just a few days ago, check it out: https://www.experts-exchange.com/questions/22734308/coldfusion-form-with-sql-server.html
Avatar of gdemaria

Simply test your field and then use NULL if it does not hold a date.

<cfset form.theDate = "">

 insert into your Table (dateField)
 values (
   <cfif isDate(form.dateField)>#createODBCdate(form.dateField)#<cfelse>NULL</cfif>
 )

 or if you prefer the cfquerparam method..

 insert into your Table (dateField)
 values (
   <cfqueryparam cfsqltype="CF_SQL_DATE" value="#form.theDate#" <cfif NOT IsDate(form.theDate)>NULL="true"</cfif> >
   )

Avatar of MFredin

ASKER

Oh IC.  I was using ' ' around NULL ('NULL').  When I removed them it worked.

Thanks.