Solved

MSSQL Server - inserting blank in datetime field gives show up as 1/1/1900 12:00:00 AM

Posted on 2011-02-28
5
556 Views
Last Modified: 2012-05-11
I need some help.  I've got a form that I created on my home system (SQL Server 2005) - works fine, but when I connected everything and ran some test inserts on my work box (also Server 2005) the dates, unless explicitly assigned a specific date, appear as 1/1/1900 12:00:00 AM.

The form has several optional fields where the registrant selects a skill (cpr, first aid, etc) and then adds a date from a calendar for the certification date.  Thus, not all dates are filled.  On the back side, I do assign anything blank a NULL but that doesn't work here for some reason - it just inserts the default date.

I can't say I've run into this before and what I've found on other sites hasn't worked - i.e. setting the default field value to null, etc.  If I set to NULL, the query works but inserts the 1900 date.  If I set it to 'NULL', I get an error about converting string to datetime and so on.

It's not a deal breaker - more of an irritant I'll have to deal with in reporting - so it would be nice to be able to get the fields without dates as null.

Can any one tell me what I'm doing wrong in this instance?

Many thanks in advance.
0
Comment
Question by:saabStory
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 25

Assisted Solution

by:reb73
reb73 earned 125 total points
ID: 34999660
A value of 0 will get translated to the value indicated, there is probably either a default in the table or something in the application connection layer that forces null values to 0.

You could get away with having a trigger or better still handle it during reporting by using NULLIF(<field>, 0)
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 125 total points
ID: 34999691
NULL will work
declare @t datetime
set @t = null
SELECT @t
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 125 total points
ID: 34999844
Go into design mode and see if the column definition has a default value of 0? If so, remove that and make sure your column is nullable.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 35000000
as indicated above: 'NULL' is not NULL , '' is the same as 0 for dates (actually what you see),
so, you have to put some code somethere to consider the case.

either in the insert/update trigger on the table, to replace the "0" date by NULL, or in the actual PHP code to use NULL and not ''
0
 

Author Comment

by:saabStory
ID: 35030398
Sorry guys - got caught up in a product launch and just buried.  Try as I might, I could not get that field to take a null.  I tried everything here and it was set properly from my end - might be something somewhere else that I couldn't see.

Nevertheless, I was realized that the dates was really just text and would never be treated as a real date for purposes of comparison or anything.  With that caveat, I just changed the data type and finished up.  Not the best of solutions but it works for my purposes at the moment.  Now that the heat is off, I can spend more time exploring this with our dba.

Many thanks to all the suggestions and examples.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql how to insert a column with the data  in an existing table 4 36
Report 8 27
how to use ROW_NUMBER() correctly 8 44
Last 4 numbers of a variable 9 22
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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