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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert null date problem 1

Status
Not open for further replies.

mmaddox

IS-IT--Management
May 22, 2002
53
US
I have an insert statement that inserts data from a form into a MS SQL table. One of the fields is a date. If the user leaves the date blank (perfectly acceptable) It inserts as 1/1/1900 rather than Null. To avoid that I am testing for no date input, then if blank it uses an insert statment that does not include the date. I would rather only have a single insert statement.

There must be a better way leave the date blank. Any Ideas?

<cfif #FORM.duedate# eq "">

<cfquery name="insertDATA2" datasource="MyDataSource">
INSERT INTO projects
(requestor)
VALUES
(
'#form.requestor#'
)
</cfquery>

<cfelse>

<cfquery name="insertDATA2" datasource="MyDataSource">
INSERT INTO projects
(requestor,duedate)
VALUES
(
'#form.requestor#',
'#form.duedate#'
)
</cfquery>
</cfif>
 
What happens if you insert Request and DUEDATE all the time? This would eliminate the IF logic. If left blank, the form.duedate field would eq "". In an Access table, the Null value would be store. Will SQL server tables allow null date values in its tables?
 
Take a look at the schema for the date field in the db table.

You may find that you are implementing a default value and/or an input mask. If so, make changes as necessary.

Otherwise, you can do the band-aid fix and run your condition inside the sql statement:

Example:
<cfquery name="insertDATA2" datasource="MyDataSource">
INSERT INTO projects
(requestor,duedate)
VALUES
(
'#form.requestor#',
<cfif IsDate(form.duedate)>'#form.duedate#'<cfelse>NULL</cfif>
)
</cfquery>
 
Thank you.
The cfif IsDate thing works as a fix.
Other info: There is no mask, no default value, and if I add data via a Microsoft Access front-end, the date field stays NULL if no data is input.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top