I have a database with a field like this (Dont know why it's varchar):
19950105
i'm trying to convert it to DATE as DDMMYYYY.
I could do this with:
convert(varchar(10), cast(The_ugly_field as date),103)
Then my date is:
05/01/1995
1) Why I can't convert it directly to date? if I use:
select convert(date,The_ugly_field,103) from Ugly_Table
it returns 1900-01-01
and it doesnt respect the 103. any number there returns the same format YYYY-MM-DD.
2)Now the real question. when I run
select convert(varchar(10), cast(The_ugly_field as date),103)
From Ugly_Table
order by The_ugly_field
after some time it returns this error:
Msg 241, Level 16, State 1, Line 29 Conversion failed when converting date and/or time from character string.
but the funny part is, it doesnt looks like the errors appears in the same row. If I run this code like 4 times, every time it returns the error, it says a different row. I mean, I execute it, then in the "results" tab I can see that it returned 1.234.555 rows
. If I run it again, after the error I return to the "result" table and now the query ran until 1.556.345
.
and please. how the hell can I convert this 20140101
in a elegant way to 2014/31/12
? Running convert(date,E1_EMISSAO,103)
doesn't seems to respect the 103
and it returns 2017-02-24
and not 24/02/2017
.
tons of questions and answers but it's all about a simple convert().
SELECT RIGHT(field,2)+'/'+MID(field,5,2)+'/'+LEFT(field,4)
date
data type does not have a format; it only acquires one when it's converted to some string representation, either viaconvert()
and such, or implicitly by the client application.convert(varchar,convert(date,UGLY_FIELD),103)
and it runs for some millions of rows and then stop. this is really strange i can't understand whats happening.