Can anyone help me please?
I need to convert this: 20140619105204
to this: 06/19/2014 10:52:04
I have tried this so far:
CONVERT(varchar, CONVERT(datetime, '20140619105204', 112), 22)
You would have to parse out the different date parts. For example:
SELECT CONVERT(varchar,
CONVERT(datetime, left('20140619105204', 8) + ' ' +
SUBSTRING('20140619105204', 9, 2) + ':' +
SUBSTRING('20140619105204', 11, 2) + ':' +
SUBSTRING('20140619105204', 13, 2), 112), 22)
Here I have used a variable to make it a bit cleaner:
DECLARE #SomeDate VARCHAR(20)
SET #SomeDate = '20140619105204'
SELECT CONVERT(varchar,
CONVERT(datetime, left(#SomeDate, 8) + ' ' +
SUBSTRING(#SomeDate, 9, 2) + ':' +
SUBSTRING(#SomeDate, 11, 2) + ':' +
SUBSTRING(#SomeDate, 13, 2), 112), 22)
Here is a SQL Fiddle
Your input string does not match any recognised date format, but as you only want output as a string, you could do it with a whole bunch of calls to SUBSTRING(...)
declare #y varchar(256) = '20140619105204'
declare #x varchar(256)
set #x = SUBSTRING(#y, 0,5) + '-' + SUBSTRING(#y, 5,2) +'-'+ SUBSTRING(#y, 7,2) +' '
+ SUBSTRING(#y,9,2) + ':' + SUBSTRING(#y,11,2) + ':' + SUBSTRING(#y,13,2)
SELECT CONVERT(varchar, cast(#x as datetime),22)
Related
I am trying to convert the time format from YYYYMMDD-HHMM and YYYYMMDD to mm/dd/yyyy hh:mm. My issue is that the data is not consistent. Below is some exemple:
20200814-1230
20200814-1230
20200814
20200814
I tried to use:
CONVERT(datetime,LEFT(id,8),120) + ' ' +
LEFT(RIGHT(ID , 6) ,2) + ':' +
SUBSTRING(RIGHT(ID , 6) , 3,2),
but the issue is that some data does not have time.
You can do it like this:
declare #dt varchar(100) = '20200814-1230'
select
substring(#dt, 5,2) + '/' +
substring(#dt, 7,2) + '/' +
substring(#dt, 1,4) +
case
when len(#dt) > 8 then
' ' +
substring(#dt, 10,2) + ':' +
substring(#dt, 12,2)
else ''
end
I have to deal with the following columns:
StartTime (nvarchar(10), null)
StartDate (nvarchar(10), null)
StartTime contains the Time as following: hhmmss
StartDate contains the Date as following: yyyymmdd (m=month)
To concatenate and convert StartDate and StartTime to a datetime i use the following code inside a stored procedure:
TRY_CAST(SUBSTRING(xy.StartDate,7,2) + '.' + SUBSTRING(xy.StartDate,5,2) + '.' + SUBSTRING(xy.StartDate,1,4) + ' ' + SUBSTRING(xy.StartTime,1,2) + ':' + SUBSTRING(xy.StartTime,3,2) + ':' + SUBSTRING(xy.StartTime,5,2) as datetime) as FULLSTARTDATE
Now imagine these values for two cases:
Case 1:
StartTime = 000000
StartDate = 20191201
FULLSTARTDATE = 2019-01-12 00:00:00.000
Case 2:
StartTime = 000000
StartDate = 20191220
FULLSTARTDATE = NULL
Why does Case 2 not output 2019-20-12? It may be a trivial issue but i can't seem to figure it out for almost an hour now.
TSQL DATETIME ISO 8601
select
TRY_CAST(SUBSTRING(xy.StartDate,1,4) + '-' + SUBSTRING(xy.StartDate,5,2) + '-' + SUBSTRING(xy.StartDate,7,2)+ 'T' + SUBSTRING(xy.StartTime,1,2) + ':' + SUBSTRING(xy.StartTime,3,2) + ':' + SUBSTRING(xy.StartTime,5,2)+'Z' as datetime) as FULLSTARTDATE,
try_cast(StartDate + ' ' + stuff(stuff(StartTime, 5, 0, ':'), 3, 0, ':') as datetime)
from
(values('20191201', '000000' ), ('20191220', '000000')) as xy(StartDate, StartTime);
It seems to me, that you are composing a date/time string in the form "DD.MM.YYYY HH:MM:SS", which seems to get interpreted as "MM.DD.YYYY HH:MM:SS" instead.
I see two possible solutions (but there might be more):
1: use "YYYY.MM.DD HH:MM:SS", which cannot be interpreted "incorrectly":
TRY_CAST(
SUBSTRING(xy.StartDate,1,4) + '.' +
SUBSTRING(xy.StartDate,5,2) + '.' +
SUBSTRING(xy.StartDate,7,2) + ' ' +
SUBSTRING(xy.StartTime,1,2) + ':' +
SUBSTRING(xy.StartTime,3,2) + ':' +
SUBSTRING(xy.StartTime,5,2) as datetime) as FULLSTARTDATE
2: use CONVERT instead of TRY_CAST:
CONVERT(DATETIME, xy.StartDate, 104) +
CONVERT(DATETIME,
SUBSTRING(xy.StartTime,1,2) + ':' +
SUBSTRING(xy.StartTime,3,2) + ':' +
SUBSTRING(xy.StartTime,5,2), 108) as FULLSTARTDATE
is it possible to convert this date time string
'20150819130706'
to something like this
'2015-08-19 13:07:06'
Here is one option:
DECLARE #DatetimeString char(14) = '20150819130706'
SELECT LEFT(#DatetimeString, 4) + '-' +
SUBSTRING(#DatetimeString, 5, 2) + '-' +
SUBSTRING(#DatetimeString, 7, 2) + ' ' +
SUBSTRING(#DatetimeString, 9, 2) +':' +
SUBSTRING(#DatetimeString, 11, 2) +':' +
RIGHT(#DatetimeString, 2)
If you want an actual datetime value, you can simply cast the entire thing to datetime:
SELECT CAST( LEFT(#DatetimeString, 4) + '-' +
SUBSTRING(#DatetimeString, 5, 2) + '-' +
SUBSTRING(#DatetimeString, 7, 2) + 'T' +
SUBSTRING(#DatetimeString, 9, 2) +':' +
SUBSTRING(#DatetimeString, 11, 2) +':' +
RIGHT(#DatetimeString, 2) As datetime)
Note: for converting to datetime you need to change the ' ' to 'T', see Lad2025's comment to this answer.
Select Convert(varchar(19),
cast(Substring('20150819130706', 1,8)
+ ' ' + Substring('20150819130706',9,2)
+ ':' + Substring('20150819130706',11,2)
+ ':' + Substring('20150819130706',13,2) as datetime),121);
Use this query.
Input is nvarchar that varies in format from m/d/yy to mm/dd/yyyy. How can this be converted to mm/dd/yyyy in SQL? So far the CONVERT() function works for the format m/d/yyyy but errors for m/d/yy.
Cast it as a date like so:
cast(#m + '/' + #d + '/' + #y as datetime)
or similarly
cast('m/d/yy' as datetime)
cast(Fieldnamehere as datetime)
update table_name set column_name = convert(varchar,cast(column_name as date),101) or if a select statment select convert(varchar,cast(column_name as date),101)
There's usually some additional nuances in the data, but here's a starter template I've been able to come up with... Just replace the debug text with convert(datetime2, [DateNVarChar], 101).
CASE
-- x/y/zzzz to xx/yy/zzzz
WHEN SUBSTRING([DateNVarChar], 2, 1) = '/' AND SUBSTRING([DateNVarChar], 4, 1) = '/' THEN
'x/y/zzzz: ' + '0' + SUBSTRING([DateNVarChar], 1, 2) + '0' + SUBSTRING([DateNVarChar], 3, 20)
-- xx/y/zzzz to xx/yy/zzzz
WHEN SUBSTRING([DateNVarChar], 3, 1) = '/' AND SUBSTRING([DateNVarChar], 5, 1) = '/' THEN
'xx/y/zzzz: ' + SUBSTRING([DateNVarChar], 1, 3) + '0' + SUBSTRING([DateNVarChar], 4, 20)
-- x/yy/zzzz to xx/yy/zzzz
WHEN SUBSTRING([DateNVarChar], 2, 1) = '/' AND SUBSTRING([DateNVarChar], 5, 1) = '/' THEN
'x/yy/zzzz: ' + '0' + [DateNVarChar]
ELSE
'Normal: ' + [DateNVarChar]
END as [FormattedDate]
Hi all i'm trying to parse an xml file in SQl Server 2005. I have a datetime field in the xml which is a string as follows: '20110805060028.387'. I want to convert this to a datetime field in sql server, but i'm struggling with the syntax. Is it possible to get dd/mm/yyy hh:mm:ss.ms (ms = micro seconds) from this string?
Its not a supported format so you will need to;
declare #dt varchar(42)= '20110805060028.387'
--convert to '20110805 06:00:28.387'
select cast(stuff(stuff(stuff(#dt, 9, 0, ' '), 12, 0, ':'), 15, 0, ':') as datetime)
>>2011-08-05 06:00:28.387
Substring is your friend here:
DECLARE #Value NVarChar(50)
SET #Value = '20110805060028.387'
SELECT Convert(DateTime,
SubString(#Value, 1, 4) + '-' +
SubString(#Value, 5, 2) + '-' +
SubString(#Value, 7, 2) + ' ' +
SubString(#Value, 9, 2) + ':' +
SubString(#Value, 11, 2) + ':' +
SubString(#Value, 13, 10))
(by the way, I am purposefully NOT following your "dd/mm/yyy" format when creating a parseable date string here, but rather an international (not quite ISO-standard) format that SQL server will read correctly regardless of the server's DATEFORMAT settings)
DECLARE #parsedValue VARCHAR(MAX)
DECLARE #rawDateValue VARCHAR(MAX)
SET #rawDateValue = '20110805060028.387'
SET #parsedValue = SUBSTRING(#rawDateValue, 1, 4) + '/' +
SUBSTRING(#rawDateValue, 5, 2) + '/' +
SUBSTRING(#rawDateValue, 7, 2) + ' ' +
SUBSTRING(#rawDateValue, 9, 2) + ':' +
SUBSTRING(#rawDateValue, 11, 2) + ':' +
SUBSTRING(#rawDateValue, 13, 2) + ':' +
SUBSTRING(#rawDateValue, 16, 3)
SELECT CONVERT(datetime, #parsedValue)
My take (necessarily not the best):
DECLARE #val VARCHAR(30)='20110805060028.387'
SELECT LEFT(#val,4) [yyyy], RIGHT(LEFT(#val,6),2)[mm], RIGHT(LEFT(#val,8),2)[dd],
RIGHT(LEFT(#val,10),2)[hh], RIGHT(LEFT(#val,12),2)[mi],
RIGHT(LEFT(#val,14),2)[ss],SUBSTRING(#val, 15,LEN(#val)-14) [ms]
DECLARE #date DATETIME
SELECT #date = CAST(LEFT(#val,4) + '-' + RIGHT(LEFT(#val,6),2) + '-' +
RIGHT(LEFT(#val,8),2) + ' ' + RIGHT(LEFT(#val,10),2) + ':' +
RIGHT(LEFT(#val,12),2) + ':' + RIGHT(LEFT(#val,14),2) +
SUBSTRING(#val, 15,LEN(#val)-14) AS DATETIME)
SELECT #date, DATEPART(MILLISECOND,#date)[ms], DATEPART(MONTH,#date)[MM]