February 23, 2016 at 1:38 am
Hello i have problem with conversion from string to date type:
In one column i have Date data like 23:02:2016, i try replace to 23022016 format, after that i try to convert to date type, but i cant i try with convert and cast, but always got error .
I try show in pictures.
SELECT CONVERT(date, Date1, 103) FROM #Peakoftheday
February 23, 2016 at 1:46 am
There is no built-in CONVERT style that will handle the format DDMMYYYY. This means that you will have to code it yourself. This should do:
CONVERT(date, RIGHT(Date1,2) + SUBSTRING(Date1, 3,2) + LEFT(Date1,2), 112)
-- Gianluca Sartori
February 23, 2016 at 2:13 am
spaghettidba (2/23/2016)
There is no built-in CONVERT style that will handle the format DDMMYYYY. This means that you will have to code it yourself. This should do:
CONVERT(date, RIGHT(Date1,2) + SUBSTRING(Date1, 3,2) + LEFT(Date1,2), 112)
Or the British version:
SELECT CONVERT(DATE,REPLACE('23:02:2016',':','/'),103) -- dd/mm/yyyy (British/French)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 23, 2016 at 2:19 am
Ah, yes, I forgot about that option. Nice catch Chris.
-- Gianluca Sartori
February 23, 2016 at 2:30 am
Thank you.... :blush:
February 23, 2016 at 3:42 pm
ChrisM@Work (2/23/2016)
spaghettidba (2/23/2016)
There is no built-in CONVERT style that will handle the format DDMMYYYY. This means that you will have to code it yourself. This should do:
CONVERT(date, RIGHT(Date1,2) + SUBSTRING(Date1, 3,2) + LEFT(Date1,2), 112)
Or the British version:
SELECT CONVERT(DATE,REPLACE('23:02:2016',':','/'),103) -- dd/mm/yyyy (British/French)
Don't actually need the REPLACE: SQL will also accept colons as delimiters in that format.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
February 24, 2016 at 2:21 am
ScottPletcher (2/23/2016)
ChrisM@Work (2/23/2016)
spaghettidba (2/23/2016)
There is no built-in CONVERT style that will handle the format DDMMYYYY. This means that you will have to code it yourself. This should do:
CONVERT(date, RIGHT(Date1,2) + SUBSTRING(Date1, 3,2) + LEFT(Date1,2), 112)
Or the British version:
SELECT CONVERT(DATE,REPLACE('23:02:2016',':','/'),103) -- dd/mm/yyyy (British/French)
Don't actually need the REPLACE: SQL will also accept colons as delimiters in that format.
Not in Britain, where we use this style code a lot:
SELECT CONVERT(DATE,'23:02:2016',103) -- dd/mm/yyyy (British/French)
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply