I have varchar '2016-07-2914:50:13.75300' and want to convert it to datetime data type.
I tried with select cast('2016-07-2914:50:13.75300' as datetime)
but I am getting
Conversion failed when converting date and/or time from character string.
Insert a space between DDHH and lose the last two MS digits:
select cast(left(stuff('2016-07-2914:50:13.75300', 11, 0, ' '), 23) as datetime)
why don't you simply use
select stuff('2016-07-2914:50:13.75300', 11, 0, ' ') ?
Different approach using SUBSTRING, of course STUFF is the way to go.
DECLARE #TIME VARCHAR(50) = '2016-07-2914:50:13.75300'
SELECT CAST(SUBSTRING(#TIME, 1, 10) AS DATETIME) +
CAST(SUBSTRING(#TIME, 11, LEN(#TIME) - 10) AS TIME)
Result:
2016-07-29 14:50:13.753
Related
I need to import a flat file into an SQL Server table and it has timestamp data that is in this format:
20171207T000131.000-0600
I have imported as a string, tried to convert, but I am not having any luck.
Well, as I wrote in the comment, SQL Server uses DateTimeOffset to store time-zone aware date and time.
The problem is that you need to translate the no-delimiters ISO 8601 format you use now to the human-readable version of ISO 8601 format - YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm], in order to convert that value into a DateTimeOffset.
You do that by adding the delimiters where they are needed, using STUFF:
Stuff inserts a new string into the existing string, starting in the specified index, instead of the existing substring of the specified length. Since you do not want to remove any existing part of the original string, you use length 0.
I've also added in my demo suggestions of ways to convert that data into date and datetime2 (Not to DateTime, there's a bug with that data type!), in case you do not need accurate information (the time zone alone can easily account for a date change):
DECLARE #DTOString varchar(100) = '20171207T000131.000-0600'
SELECT CAST(LEFT(#DTOString, 8) As Date) As [Date],
CAST(
STUFF(
STUFF(
STUFF(
LEFT(#DTOString,19)
, 14, 0, ':')
, 12, 0, ':')
,9, 1, ' ') -- Replacing the T with a space
As DateTime2) AS [DateTime2], -- NOT to DateTime! there's a bug!
CAST(
STUFF(
STUFF(
STUFF(
STUFF(
STUFF(#DTOString, 23, 0, ':')
, 14, 0, ':')
, 12, 0, ':')
, 7, 0, '-')
, 5, 0, '-')
As DateTimeOffset) As [DateTimeOffset]
Result:
Date DateTime2 DateTimeOffset
07.12.2017 07.12.2017 00:01:31 07.12.2017 00:01:31 -06:00
In case you need only the date import it as a string and execute the following query
select cast(substring('20171207T000131.000-0600',1,8) as date)
i am extracting '20171207' and converting it to date
Try this:
SELECT CONVERT(DATETIME,
STUFF(STUFF(STUFF(STUFF(STUFF(STUFF('20171207T000131.000-0600',20,5,''),14,0,':'),12,0,':'),9,1,' '),7,0,'-'),5,0,'-'),120)
My table TRANS contains T_STAMP column with the value '20170721154922' is a YYYYMMDDHHMMSS format. I am trying to write a query to display as MM/DD/YY in one column and HH:MM:SS in another column.
I am trying to convert this with the method CONVERT(VARCHAR(10),GETDATE(),10)
but I need full snippet to execute.
Could you help .
The following query can be return the exact format you required, the date as MM/DD/YY and time as HH:MM:SS:
SELECT CONVERT(VARCHAR(8), CONVERT(DATE, LEFT(T_STAMP, 8)), 1) AS DateValue,
CONVERT(VARCHAR(8), CONVERT(TIME,
SUBSTRING(T_STAMP, 9, 2) + ':'
+ SUBSTRING(T_STAMP, 11, 2) + ':'
+ SUBSTRING(T_STAMP, 13, 2)), 8) AS TimeValue
FROM Trans
Output:
DateValue | TimeValue
-----------|-----------
07/21/17 | 15:49:22
The following query will convert a value from the given format (YYYYMMDDHHMMSS) to DATE and TIME
DECLARE #T_STAMP NVARCHAR(100)='20170721154922'
SELECT CONVERT(DATE,LEFT(#T_STAMP,8)) T_DATE
,CONVERT(TIME,SUBSTRING(#T_STAMP,9,2)+':'
+SUBSTRING(#T_STAMP,11,2)+':'
+SUBSTRING(#T_STAMP,13,2)) T_TIME
As you mentioned the values are stored in YYYYMMDDHHMMSS format, so hope the values always be in fixed length (length 14).
Note:- As commented by Jens, Storing timestamps as formated strings is no good idea. Better store it in a timestamp datatype.
SELECT FORMAT(GETDATE() , 'MM/dd/yyyy HH:mm:ss')
the above would return something like this(21/07/2017 16:02:20).
select date_format(str_to_date('20170721154922', '%Y%m%d%H%S'), '%m%d%y');
'20170721154922' is a very very bad format to store date...
DECLARE #d varchar(14) = '20170721154922';
DECLARE #date DATETIME = CONVERT(datetime2, LEFT(#d, 8), 112);
DECLARE #time DATETIME = CONVERT(datetime2, CONCAT(SUBSTRING(#d, 9, 2), ':', SUBSTRING(#d, 11, 2), ':', SUBSTRING(#d, 13, 2)), 108);
SELECT FORMAT(#date, 'MM/dd/yy') date, FORMAT(#time, 'HH:mm:ss') time
I Have a string like as given below
201004301342
Need to convert it into dd/mm/yy format
Can anyone help me please?
This will change the value to a datetime. A datetime has no format until you convert back to a string again.
You can use stuff to change the string value to 20100430 13:42 and then cast to datetime.
declare #Date varchar(12)
set #Date='201004301342'
select cast(stuff(stuff(#Date, 11, 0, ':'), 9, 0, ' ') as datetime)
if you want a string and start string is always with this format then
declare #date varchar(50)
set #date = '201004301342'
SELECT convert(varchar, cast(substring(#date, 1, 8) as datetime), 103)
I have searched on this but my string seems to be in a different format to the examples I can find.
I have a time set as a nvarchar(50) and values such as 1535
I have two of these columns and I want to compare the two to find how many minutes difference there is. How do I convert the string to a time?
One way;
declare #t1 nvarchar(4) = '1535'
declare #t2 nvarchar(4) = '1700'
select
datediff(minute,
cast(stuff(#t1, 3, 0, ':') as time),
cast(stuff(#t2, 3, 0, ':') as time))
>>85
If you need the difference in minutes, this is a method without converting your values to Datetime.
Assuming all nvarchar values are convertible to int and they are in 24h format, difference of c2-c1 in Minutes:
Fiddle demo here
select ((c2*1-c1*1)/100)*60 + (c2*1-c1*1)%100 inMinutes
from t
Assuming all your values can be convertible to Time:
DECLARE #val1 as NVARCHAR(50);
DECLARE #val2 as NVARCHAR(50);
SET #val1 = '1535'; SET #val2 = '1655';
SELECT DATEDIFF(minute,convert(time,LEFT(#val1,2)+':'+RIGHT(#val1, 2)+':00'), CONVERT(time,LEFT(#val2,2)+':'+RIGHT(#val2, 2)+':00'))
Does anybody now how to convert this character to date or datetime:
'10022011'
Quick & dirty:
select convert(datetime, stuff(stuff('10022011', 5,0,'-'),3,0,'-'))
However, you might want to consider converting the string into ISO standard date format:
declare #d char(8)
select #d = '10022011'
select convert(datetime, substring(#d,5,4) + '-' + substring(#d,3,2) + '-' + substring(#d, 1, 2))
in order to avoid ambiguity. 10-02-2011 has different meanings depending on which side of the pond you are.
SELECT convert(datetime, STUFF(STUFF('10022011',3,0,'-'),6,0,'-'), 103)
the number at the end is the Sql format you want the date to output