November 19, 2013 at 7:52 am
Hi,
DB version: MS SQL server 2008 R2
I have a date field which stores date as numeric.
How can I convert it from numeric to date format
Date is stored in YYYYMMDD format ,datatype is numeric(8,0)
the field also has nulls and 0's
.
I tried using
CONVERT(DATETIME, CAST(I290.[MYDATEFIELD] AS VARCHAR(8)), 112)AS NEWDATE
This is the error i get
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Thanks
SM
November 19, 2013 at 7:56 am
the field also has nulls and 0's
What date would you like 0 converted to?
John
November 19, 2013 at 7:59 am
Hey John,
For 0's,invalid dates it should display nulls
Thanks
SM
November 19, 2013 at 7:59 am
Try with:
CAST(CAST( NULLIF(NumericDatefield, 0) AS char(8)) AS datetime)
November 19, 2013 at 8:07 am
You are the man :-)!! Luis
Perfect that's what I was looking for
November 19, 2013 at 8:08 am
And then it would be HIGHLY advisable to change your schema that you store dates in a date column instead of numeric. It will save you countless hours of pulling out your hair because you got garbage into your database that is unable to be converted to the datatype it should be in the first place.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 19, 2013 at 8:29 am
Sean Lange (11/19/2013)
And then it would be HIGHLY advisable to change your schema that you store dates in a date column instead of numeric. It will save you countless hours of pulling out your hair because you got garbage into your database that is unable to be converted to the datatype it should be in the first place.
+1
Everyday I struggle because we have dates in int, char(8) & char(10) datatypes. It's painful but I can't change it.:(
November 19, 2013 at 8:31 am
Same here!!!:rolleyes:
November 19, 2013 at 8:43 am
We all have those things we can't change and often they are the most frustrating. Hopefully you at least realize how awful of a design that is and don't do it yourself because you have seen just how painful it can be. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 20, 2013 at 4:31 pm
Heh... I remember some of the original computer games. If you can't move left, right, forward, back, up, or down, then change color!
Add a persisted computed column to the table with Luis' formula. If the app was written incorrectly, the app will break and you can just drop the column to "fix" it. If the app doesn't break, then you have the best of both worlds.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply