Solved

sql query for null value

Posted on 2016-07-26
2
26 Views
1 Endorsement
Last Modified: 2016-07-26
Hello,


Datetime column inserting 1/1/1900 for null value

Have a code ,which does job for time but not date

[PickUpTime]  = CASE when @PickUpTime= '' then NULL else CAST(@PickUpTime  AS  nvarchar(20)) end ,  


Cheers
1
Comment
Question by:RIAS
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41729520
Eyeball this table in table design and see if the column is defined NOT NULL, and if so you'll need to change it to allow NULLs.

Right-click on table > Script Table As ... > Create To > New Query Window Editor..

-- Change the column definition to allow NULLs
ALTER TABLE YourTableName
ALTER COLUMN PickUpTime datetime NULL

Open in new window


>Datetime column
>CAST(@PickUpTime  AS  nvarchar(20)) end ,  
So which is it ... a datetime column or a nvarchar(20)) column?  Storing date values in a varchar column is a poor programming practice as it will allow all sorts of values that are not convertable to dates, which will throw an error.
1
 

Author Closing Comment

by:RIAS
ID: 41729527
Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

757 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

17 Experts available now in Live!

Get 1:1 Help Now