Solved

Microsoft, SQL, SQL 2005 - Query Assistance

Posted on 2007-11-16
6
200 Views
Last Modified: 2010-05-18
I need help recoding this one segment .....

I am currently getting back a '1900-01-01 00:00:00.000' when the ELSE IsNull(pi.birthdate,'') should have reported back nothing. Can someone assist me?

'128.Insured1DOB' = 

		CASE  WHEN COALESCE(pi.InsuredSameAsPatient,1) = 1 THEN pp.birthdate

		WHEN COALESCE(pi.InsuredSameAsGuarantor,1) = 1 THEN g.birthdate

		ELSE IsNull(pi.birthdate,'') END

Open in new window

0
Comment
Question by:Jeff S
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 20299763
A date of '' is 1900-01-01, since that is the default date in SQL Server.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 20299789
CASE  WHEN COALESCE(pi.InsuredSameAsPatient,1) = 1 THEN CONVERT(VARCHAR(10),pp.birthdate,120)
            WHEN COALESCE(pi.InsuredSameAsGuarantor,1) = 1 THEN CONVERT(VARCHAR(10),g.birthdate,120)
            ELSE IsNull(pi.birthdate,'') END
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 20299792
Such as this:
SELECT CAST('' AS DATETIME)

To leave it truly blank, you will need to force the dates to varchar so that an empty string can be shown.  For example:
CASE  WHEN COALESCE(pi.InsuredSameAsPatient,1) = 1 THEN CONVERT(VARCHAR(10), pp.birthdate, 101)

		WHEN COALESCE(pi.InsuredSameAsGuarantor,1) = 1 THEN CONVERT(VARCHAR(10), g.birthdate, 101)

		ELSE IsNull(CONVERT(VARCHAR(10), pi.birthdate, 101), '') END

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20299801
Since the case statement is returning datetime values in case  birthDate is null, sql will try to convert the value '' to datetime implicitly yielding the default datetime valuw od 1900-01-01

SELECT CAST('' AS DATETIME)
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 20299805
You should put a Null in the Birdate Field -  I presume that it's a data datatype? - these are sotred internally as numbers and can't contain a blank.
I guess your code actually has the effect of storing a zero . Data fields store the data as an offset in days from 1900-01-01, so tha's what zero signifies.
0
 
LVL 7

Author Comment

by:Jeff S
ID: 20299853
Thanks a million ..... just the result I needed and awesome explanations! Thanks to all who helped.
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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2 - Execution Plan 3 31
SQL Select Query help 3 32
encyps queries mssql 15 27
sql query Help 12 34
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

13 Experts available now in Live!

Get 1:1 Help Now