|
I am trying to run a script to get customer's arrival, departure, date reservation made from a table but these column fields have numeric(6,0) datatype. It is MMDDYY. Here is 2 sample of these fields value:- 103110, 110110 And, I can't get it to convert to datetime. Any ideas please. I keep getting this error:-
(comments are locked)
|
|
You can use the American date standard (mm/dd/yy), by specifying style 1 on the convert function, after adding the appropriate '/' characters. this gives ---- Edit : Oleg makes a good point about the date only being 5 chars if the month is less than 10, so here is a revised solution gives +1 - but I went for
Nov 05 '10 at 01:59 AM
Matt Whitfield ♦♦
Yep there are sooooooooo many ways
Nov 05 '10 at 02:00 AM
Kev Riley ♦♦
I think I am late to the train, but it seems that the key piece of information is disregarded here. That key is that the data is stored as decimal(6, 0) or how it reads in question numeric(6,0). This does mean that should the month part be earlier than October, the total number of characters would be 5 not 6, which will then break the statement in the answer: Msg 242, Level 16, State 3, Line 7 The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. Because of it, Matt's way appears safer because it is resilient to the problem with 5 digits instead of 6. Additionally, if to modify it slightly to append '20' or '19' depending on the year to the left then his convert results in the YYYYMMDD, which is independent of any local settings and does not need a format parameter: select convert(datetime, '20' + convert(varchar, floor([resdate] / 100) + ((resdate % 100) * 10000))) from @dates; Unfortunately, the statement above is not resilient to the problem of the date where year part is less than 10, such as 103105 This means that the answer still requires further tweaking :)
Nov 05 '10 at 08:51 AM
Oleg
@Oleg - good spot... '20' + Right('000000' + ... , 6) ... or something similar?
Nov 05 '10 at 09:04 AM
Matt Whitfield ♦♦
the moral of the story is: if you are going to use a number to represent a date, such as an int key for a time dimension, always use YYYYMMDD. It is an ISO standard and you get the added benefit of natural ordering of dates
Nov 05 '10 at 06:37 PM
Scot Hauder
(comments are locked)
|
|
Using ISO 8601 format should make it work regardless of regional settings, and on any DBMS that speaks ISO SQL. Format is YYYY-MM-DDThh:mm:ss[.nnn] In your case '2010-10-31T00:00:00' In code: I'm pretty sure that would work in any ISO sql dialect. @Magnus Ahlkvist Yes, it will work in any dialect. There is another ISO format independent of local settings, it is YYYYMMDD. It will not work, however, when the month part is earlier than October, because the original data is stored as numeric(6,0) meaning that it will only have 5 not 6 digits for those values.
Nov 05 '10 at 08:03 AM
Oleg
Good point. Good fix too from Kev.
Nov 05 '10 at 03:07 PM
Magnus Ahlkvist
(comments are locked)
|
|
Or ... :)
(comments are locked)
|
|
Please try this - @Leo Same problem, this does not work if the month is earlier than October. Try DECLARE @IntDate INT = 53110 to see. Kev updated his answer to cure the problem, check it out :)
Nov 05 '10 at 10:05 AM
Oleg
@Oleg - yes, that is right. Sorry I missed that. I hope that will work
Nov 08 '10 at 04:52 AM
Leo
@Leo It works. In case if you need to compact all the logic into a single statement (because the original question was asking about values in the table), you will have to opt for: select stuff(stuff(
case
when len(resdate) < 6 then '0' +
convert(varchar(6) ,resdate)
else convert(varchar(6) ,resdate)
end, 3, 0, '/'), 6, 0, '/')
from @dates;Kev was exactly right when he said: Yep there are sooooooooo many ways
Nov 08 '10 at 08:47 AM
Oleg
(comments are locked)
|


Thanks so much for all the wonderful replies. It works for the select part of the query but not on the where clause. For example, select stuff(stuff( case when len(DATE_RES_MADE) < 6 then '0' + convert(varchar(6) ,DATE_RESERVATION_MADE) else convert(varchar(6) ,DATE_RESERVATION_MADE) end, 3, 0, '/'), 6, 0, '/') from RES where DATE_RES_MADE > DATEADD(dd, DATEDIFF(dd, 0, GETDATE())-1, 0);
@sharon This is simply because your original DATE_RES_MADE is still numeric(6,0), and if you need the predicate to work as date comparison, you can do something like this:
select records.DATE_RES_MADE from ( select stuff(stuff( case when len(DATE_RES_MADE) < 6 then '0' + convert(varchar(6) ,resdate) else convert(varchar(6) ,resdate) end, 3, 0, '/'), 6, 0, '/') as DATE_RES_MADE from RES ) records where records.DATE_RES_MADE > dateadd(dd, datediff(dd, 0, getdate()) -1, 0);Otherwise, you will have to copy the select part into the where clause in place of your DATE_RES_MADE.