Need to insert null into date field

Posted on 2007-08-05
Last Modified: 2013-12-24
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?
Question by:MFredin
    LVL 35

    Accepted Solution


    if you use the word NULL in your string ie

    insert into yourTable (yourDate, yourOtherField) values (NULL, "ASDF")

    then the date field would be null otherwise if you put a normal date in then it should work,

    I'm not sure what code that you are using - additionally, your table needs to have allow nulls for this field on this table.
    LVL 10

    Expert Comment

    There was a topic on this just a few days ago, check it out:
    LVL 39

    Expert Comment


    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> >


    Author Comment

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


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now