SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem Converting date format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Niki
Yak Posting Veteran

51 Posts

Posted - 11/14/2012 :  12:45:20  Show Profile  Reply with Quote
In my SELECT statement, I am converting the various dates based on a code in another column like this

FEP_DATE =
case EnCode
when '3' then CONVERT(nvarchar(10), LepDate,112)
when '4' then CONVERT(nvarchar(10), recdate,112)
when '2' then CONVERT(nvarchar(10), SEntry,112)
when '1' then convert(datetime, NULL)
end

LepDate is datetime format 2002-01-17 00:00:00 & I want to convert it to yyyymmdd format. It does not in above example. But CONVERT functions works fine if tested by itself on this date. What am I doing wrong in my CASE statement?

Niki

TG
Flowing Fount of Yak Knowledge

USA
6059 Posts

Posted - 11/14/2012 :  13:01:09  Show Profile  Reply with Quote
change this:
when '1' then convert(nvarchar(10), NULL)
all cases should return the same datatype


Be One with the Optimizer
TG
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 11/14/2012 :  13:01:36  Show Profile  Reply with Quote
Try changing this
when '1' then convert(datetime, NULL)
to
when '1' then convert(nvarchar(10), NULL)
The Case statement must reurns values of the same datatype


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 11/14/2012 :  13:03:35  Show Profile  Reply with Quote
quote:
Originally posted by TG

change this:
when '1' then convert(nvarchar(10), NULL)
all cases should return the same datatype


Be One with the Optimizer
TG



Wow! It's been a while since i was nsiped like that!

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6059 Posts

Posted - 11/14/2012 :  13:05:33  Show Profile  Reply with Quote


Be One with the Optimizer
TG
Go to Top of Page

Niki
Yak Posting Veteran

51 Posts

Posted - 11/14/2012 :  13:07:02  Show Profile  Reply with Quote
Wow! Thanks a bunch! it worked!

Niki
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000