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
Solved

Microsoft, SQL, SQL 2005 - Query Assistance

Posted on 2007-11-16
6
225 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:Scott Pletcher
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:Scott Pletcher
Scott Pletcher 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 to return specific rows and columns, with various degrees of sorting and limits in place.

807 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