Convert varchar (eg. 20151125175706+0800) to Datetime in SQL - sql-server

How do I convert yyyymmddhhmmss+offset (20151125175706+0800) to datetime format in SQL Server? To something that can be used in datetime comparison to select records entered in table during last one minute.
Im getting this format from OBX segment of the message and I'm saving this as is in a varchar field in table.
I tried to do this(among other things):
select cast(convert(varchar, SUBSTRING(OBXDateTime, 1, 4) + '-' +
SUBSTRING(OBXDateTime, 5, 2) + '-' +
SUBSTRING(OBXDateTime, 7, 2) + ' ' +
SUBSTRING(OBXDateTime, 9, 2) + ':' +
SUBSTRING(OBXDateTime, 11, 2) + ':' +
SUBSTRING(OBXDateTime, 13, 2), 101) as datetime) from ObservationPatInfo
This query gives only 522 rows out of 1020 rows in the table and gives as error message saying
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
Kindly help me to solve this.

You can try this:
DECLARE #YourDt VARCHAR(100)='20151125175706+0800';
DECLARE #ISO8601_with_TimeZone VARCHAR(100);
--This will use multiple STUFFs to get the separators into your string
SELECT #ISO8601_with_TimeZone = STUFF(STUFF(STUFF(STUFF(STUFF(STUFF(#YourDt,18,0,':'),13,0,':'),11,0,':'),9,0,'T'),7,0,'-'),5,0,'-');
--This looks like a properly formatted datetime now: 2015-11-25T17:57:06+08:00
SELECT #ISO8601_with_TimeZone;
--Use DATETIME2 and code 127 to convert ISO8601 with TimeZone
SELECT CONVERT(datetime2,#ISO8601_with_TimeZone,127);

Use TRY_CONVERT to find the bad values:
select pi.*
from (select try_convert(ddatetime,
(SUBSTRING(OBXDateTime, 1, 4) + '-' +
SUBSTRING(OBXDateTime, 5, 2) + '-' +
SUBSTRING(OBXDateTime, 7, 2) + ' ' +
SUBSTRING(OBXDateTime, 9, 2) + ':' +
SUBSTRING(OBXDateTime, 11, 2) + ':' +
SUBSTRING(OBXDateTime, 13, 2)
), 101) as datetime) as dte, pi.*
from ObservationPatInfo pi
) pi
where dte is null;

Related

SQL Server error: Conversion failed when converting date and/or time from character string

I am trying to write this code, but I keep getting this error. AUSVN and AUZTV columns are of type nvarchar. Please help me.
SELECT
CONVERT(DATETIME, (CONVERT(VARCHAR, CONVERT(DATE, AUSVN)) + ' ' + (LEFT(AUZTV, 2) + ':' + SUBSTRING(AUZTV, 3, 2) + ':' + RIGHT(AUZTV, 2))))
FROM
VIQMEL
AUSVN column has values like 2018-04-27 14:20:18.000.

timestamp from sap into datetime in sql

from the beginning. I extracted data from Sap to MYSQL DB. In some tables, there are columns that were extracted as FLOAT and looks like this: 20131009012152.
As you can see it's like the string but float.
If I try to convert it into datetime, I get errors or overload etc.
I have tried CAST, CONVERT, SRT, SUBSTRING, nothing works.
Last try:
SELECT top 10 CREATED_AT,
SUBSTRING(CAST(STR(CREATED_AT, 25, 5) as varchar), 1, 4)
from databank.tablename;
-- wanted to substract parts (here, year) but I get just empty column as result.
Cast to nvarchar to datetime doesn't work. as well as nvarchar- bigint - datetime.
Hope, somebody can help me, thanks
One option, rather ugly, uses the CONVERT function with a series of string concatenations. I first get your numeric timestamp over to a string using a CTE.
WITH cte AS (
SELECT CAST(20131009012152 AS VARCHAR) num
)
SELECT
CONVERT(datetime, SUBSTRING(num, 1, 4) + '-' + SUBSTRING(num, 5, 2) + '-' +
SUBSTRING(num, 7, 2) + ' ' + SUBSTRING(num, 9, 2) + ':' +
SUBSTRING(num, 11, 2) + ':' + SUBSTRING(num, 13, 2), 120) AS the_date
FROM cte;
The verbosity of this query and your data makes me think that you probably did not export properly. Use this only if SAP really has no ability to export a proper timestamp (which I doubt).
Output:
09.10.2013 01:21:52
Demo
Found the similar way that can be directly used in SELECT:
CONVERT(datetime,
SUBSTRING(CAST(STR(COLUMN, 15) as varchar), 1,5) + '-' +
SUBSTRING(CAST(STR(COLUMN, 15) as varchar),6, 2) + '-' +
SUBSTRING(CAST(STR(COLUMN, 15) as varchar), 8, 2) + ' ' +
SUBSTRING(CAST(STR(COLUMN, 15) as varchar),10, 2) + ':' +
SUBSTRING(CAST(STR(COLUMN, 15) as varchar), 12, 2) + ':' +
SUBSTRING(CAST(STR(COLUMN, 15) as varchar), 14, 2), 120) as [COLUMN]
Result: 2017-01-01 15:33:15.000
Besides an old threat I want to share my solution:
Normally the SAP-Timestamps come like this in a char or varchar: 20220620081129
To bring it into a usable SQL format you need to stretch it into a date/datetime
format with dashes and colons.
This can be easily achieved by converting the text into a int/bigint and then
formatting this number into an output format that looks like a date.
Next step is just converting this resulting text into a datetime.
/*Code:*/
convert(datetime,format(convert(bigint,<fieldname>),'##-##-## ##:##:##'),120)
/*Example*/
DECLARE #SAPTable AS TABLE ([/BIC/XCREATE] VARCHAR(30))
INSERT INTO #SAPTable ([/BIC/XCREATE])
VALUES ('20220822144737');
SELECT convert(DATETIME, format(convert(BIGINT, [/BIC/XCREATE]), '##-##-## ##:##:##'), 120)
FROM #SAPTable
As easy as this.
Just adjust the fieldname [/BIC/XCREATE] and the output format to your needs.
In case you have other (shorter or longer) datetime values then, for sure, some things are needed to be adjusted but the principle is the same.
At the end the result is a real date value in terms of the SQL-Server and can be used anyhow.
I think that way it's easier than fiddling with substrings.

SQL Server 2008 : Varchar to Datetime conversion

I have a datetime column ArrivalDateTime which is stored as a varchar value.
Let's say if the value is 20161212093256, I want the output to be 2016-12-12 09:32:56.
I could get the date part in datetime format as below.
SELECT
CONVERT(DATETIME2(0), LEFT('20161212093256', 8))
This returns the output as 2016-12-15 00:00:00.
I tried the following query to get the time part as well.
SELECT
CONVERT(DATE, LEFT('20161212093256', 8)) + ' ' +
CONVERT(TIME, RIGHT('20161212093256', 6))
But this throws an error:
The data types date and varchar are incompatible in the add operator
How can I get both date and time part in datetime format?
Get the date component first and convert it to DATETIME and then get the time component and convert it to DATETIME also. Finally, add the two results:
SELECT
CONVERT(DATETIME,LEFT('20161212093256', 8)) +
CONVERT(DATETIME,
LEFT(RIGHT('20161212093256', 6), 2) + ':' +
SUBSTRING(RIGHT('20161212093256', 6), 3, 2) + ':' +
RIGHT(RIGHT('20161212093256', 6), 2)
)
To further explain, the result first conversion is the date component:
2016-12-12 00:00:00.000
The second conversion is the time component, but when you convert it to DATETIME it adds it to the 0 date or '1900-01-01', so the result is:
1900-01-01 09:32:56.000
Then, you add both DATETIMEs to get:
2016-12-12 09:32:56.000
To get rid of the ms component:
SELECT
CONVERT(DATETIME,LEFT('20161212093256', 8)) +
CONVERT(DATETIME,
LEFT(RIGHT('20161212093256', 6), 2) + ':' +
SUBSTRING(RIGHT('20161212093256', 6), 3, 2) + ':00'
)
Try this,
DECLARE #V_STR VARCHAR(20) = '20161212093256'
SELECT CONVERT(SMALLDATETIME,LEFT(#V_STR,8) +' '+ --date
SUBSTRING(#V_STR,9,2)+':'+ --hour
SUBSTRING(#V_STR,11,2)+':'+ --minute
SUBSTRING(#V_STR,13,2)) AS DATE_TIME --second
Try this
select concat(CONVERT(DATE, LEFT('20161212093256', 8)) , ' ' , CONVERT(TIME, substring(RIGHT('20161212093256', 6),1,2)+ ':' + substring(RIGHT('20161212093256', 4),1,2) + ':' +RIGHT('20161212093256', 2)))
above will display time with miliseconds, below without miliseconds
select concat(CONVERT(DATE, LEFT('20161212093256', 8)) , ' ' , substring(RIGHT('20161212093256', 6),1,2)+ ':' + substring(RIGHT('20161212093256', 4),1,2) + ':' +RIGHT('20161212093256', 2))
SELECT STUFF(STUFF(STUFF(STUFF(STUFF('20161212093256', 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':')

Converting SQL Server varchar to datetime for arithmetic

I want to combine two separate columns into date time and subtract 20 hours in SQL Server.
This works in Oracle:
create table t ( x varchar2(8),y varchar2(6));
insert into t values ('20151106','090000');
select to_char(to_date(x||' '||y, 'yyyymmdd hh24miss')
- (20/24),'dd-mm-yyyy hh24:mi:ss') as "date minus 20 hrs" from t;
date minus 20 hrs
- -----------------
05-11-2015 13:00:00
I can't crack it in SQL Server, I can get parts to work but can't combine the two dates (too many functions).
This works:
CREATE TABLE t (x VARCHAR(8), y VARCHAR(6))
INSERT INTO t
VALUES('20151105', '150800')
SELECT
DATEADD(HOUR, -20, CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2), ':', SUBSTRING(y, 5, 2)) AS TIME))
FROM t
This works:
SELECT DATEADD(HOUR, -20, x)
FROM t
But I can't combine them:
SELECT
DATEADD(HOUR, -20,
CONCAT(SUBSTRING(x, 1, 4), '-', SUBSTRING(x, 5, 2), '-',
SUBSTRING(x, 7, 2), ' ',
CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2),
':', SUBSTRING(y, 5, 2)) AS TIME)))
FROM t
If you CONVERT the values properly to a datetime, there should be no issue with what you're trying to do. You want to give SQL Server a date in the following format, or one of the other acceptable formats:
YYYYMMDD HH:MM:SS
So applying that to your sample, the date portion is fine, but you need to get the time format correct before you can convert it to a valid date:
CREATE TABLE #t ( x VARCHAR(8), y VARCHAR(6) )
INSERT INTO #t
VALUES ( '20151105', '150800' )
-- we want 20151105 15:08:00
SELECT DATEADD(HOUR, -20,
( CONVERT(DATETIME, x + ' ' +
LEFT(y, 2) + ':' +
SUBSTRING(y, 3, 2) + ':' +
RIGHT(y, 2)) )) AS Result
FROM #t
DROP TABLE #t
Produces
2015-11-04 19:08:00.000
The string you're passing to datepart evaluates to:
select dateadd(hour, -20, '2015-11-05T15:08:00.0000000')
But you'll get a Conversion failed error for more than 3 zeroes. An easy way to fix that is to remove the second column's conversion to time. While you're at it, you could use + instead of concat, which is easier to read:
select dateadd(hour, -20,
substring(x,1,4) + '-' +
substring(x,5,2) + '-' +
substring(x,7,2) + 'T' +
substring(y,1,2) + ':' +
substring(y,3,2) + ':' +
substring(y,5,2))
from t
See it working at SE Data.

Format short int to time in SQL Server

I have a time column showing up as an int. 800 would be 08:00am and 1300 would be 1:00pm.
How can I format the int so that it appears as 12h time, e.g. 1:13 pm?
I'm using SQL Server.
Thanks
You can do something like below to achieve the required result. Idea taken from How to convert an integer (time) to HH:MM:SS::00 in SQL Server 2008?
set #time = 1300
select (#time / 1000000) % 100 + ':' +
(#time / 10000) % 100 + ':' +
(#time / 100) % 100 + ':' +
(#time % 100) * 10
Convert the integer to hh:mm:ss representation of datetime and handle formatting (i.e. AM/PM) in the presentation layer:
DECLARE #Time INTEGER = 800;
SELECT
-- convert datetime to hh:mm:ss
CONVERT(VARCHAR,
-- cast hh:mm to datetime
CAST(-- insert semicolon to get hh:mm format
STUFF(-- convert integer to string and pad with zeroes
RIGHT(REPLICATE('0', 4) + CAST(#Time AS VARCHAR), 4),
3, 0, ':')
AS DATETIME),
108);
SELECT #Time = 1300;
SELECT CONVERT(VARCHAR, CAST(STUFF(RIGHT(REPLICATE('0', 4) + CAST(#Time AS VARCHAR), 4), 3, 0, ':') AS DATETIME), 108);
Example selecting from temporary table:
CREATE TABLE #Time ( TimeValue INTEGER );
INSERT INTO #Time VALUES (800);
INSERT INTO #Time VALUES (1300);
SELECT CONVERT(VARCHAR,
CAST(STUFF(RIGHT(REPLICATE('0', 4) + CAST(TimeValue AS VARCHAR), 4),
3, 0, ':')
AS DATETIME), 108) AS Time
FROM #Time;
DROP TABLE #Time;

Resources