Convert string to datetime - SQL - sql-server

I have a string parsed from XML which represents datetime.
String format is: '20200915114000' - (YYYYMMDDhhmmss)
Is there a function in SQL Server to convert or parse this string to datetime or should I split string manually and concatenate it into datetime?

How to do this, in 3 steps:
C:\> SQLCMD
1> select convert(datetime,'20200915114000' )
2> go
Msg 241, Level 16, State 1, Server ZES, Line 1
Conversion failed when converting date and/or time from character string.
1> select convert(datetime,'20200915 114000' )
2> go
Msg 242, Level 16, State 3, Server ZES, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
1> select convert(datetime,'20200915 11:40:00' )
2> go
-----------------------
2020-09-15 11:40:00.000
(1 rows affected)
1>
Conclusion you need to add a space and 3 ':' in the string to convert '20200915114000' to '20200915 11:40:00'. After this a simple CONVERT will do the trick.

Solution to my problem:
declare #sDate char(14), #sDateOK char(20)
set #sDate = '20200915114000'
set #sDateOK = substring(#sDate,1,8) + ' ' + substring(#sDate,9,2) + ':' + substring(#sDate,11,2) + ':' + substring(#sDate,13,2)
select convert(datetime,#sDateOK ) as Date

Related

SQL Server : convert nvarchar type to INT

I need to convert a NVARCHAR(MAX) column which has values as shown below to INTEGER.
I'm trying this code which isn't working:
SELECT CONVERT(INT, CriteriaValue)
FROM OUTPUT_KEY
Table has these values:
£19,000,000
£10,000,000
£10,000,000
I get this error:
Msg 245, Level 16, State 1, Line 23
Conversion failed when converting the nvarchar value '£10,000,000' to data type int.
The following also doesn't work
DECLARE #ConvertValue AS NVARCHAR(50)
SET #ConvertValue = '19,000,000'
SELECT CONVERT(INT, #ConvertValue)
Error:
Msg 245, Level 16, State 1, Line 25
Conversion failed when converting the nvarchar value '19,000,000' to data type int.
The value you are trying to convert to an integer contains a comma and pound symbol. SQL Server is unable to handle the implicit conversion for you because of that.
You can read all about that here:
https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-type-conversion-database-engine?view=sql-server-2017
In this specific example you'll need to remove those characters prior to the conversion. Here's an example using a nested REPLACE if all you have is the pound and comma:
DECLARE #ConvertValue AS NVARCHAR(50);
SET #ConvertValue = '£19,000,000';
SET #ConvertValue = REPLACE(REPLACE(#ConvertValue, ',', ''), '£', '');
SELECT #ConvertValue;
If your resulting data type was money, SQL would implicitly handle the conversion for you:
DECLARE #ConvertValue AS NVARCHAR(50);
SET #ConvertValue = '£19,000,000';
SELECT CONVERT(MONEY, #ConvertValue);
Based on sample data & query, you need REPLACE() to make it work :
DECLARE #ConvertValue nvarchar(50)
SET #ConvertValue = '19,000,000'
SELECT CONVERT(INT, REPLACE(#ConvertValue, ',', ''))
If the #ConvertValue has £ also , then you need one more REPLACE() :
SELECT CONVERT(INT, REPLACE(REPLACE(#ConvertValue, '£', ''), ',', ''))
Maybe the Money Data Type would work better for you
Declare #ConvertValue as nvarchar(50)
SET #ConvertValue = '$19,000,000'
SELECT convert (money, #ConvertValue)

Conversion failed when converting date and/or time from character string in SQL Server - why?

SET #tableHTML =
#tableHTML +N'<tr><td colspan="2"> 1st email call T -3 = ' + DATEADD(day,-3, #Leaguedate) + '</td></tr>'
I'm getting this error message:
Conversion failed when converting date and/or time from character string.
You must convert the datetime value to string (char/varchar/nchar/nvarchar).
The following statement:
SELECT 'string ' + DATEADD(day,-3, GETDATE()) +' another string'
Will result with:
Conversion failed when converting date and/or time from character string.
However this statement:
SELECT 'string '+ CONVERT(char(10), DATEADD(day,-3, GETDATE()), 103) +' another string'
Will result with:
string 06/03/2018 another string

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

I am creating a procedure in Microsoft SQL Server 2012 but I have a problem. The error message is:
Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
Here is my procedure:
DECLARE #name varchar(255)
SELECT name
FROM sysobjects
WHERE type = 'P'
AND (name LIKE 'pr_My_app_log_%' OR name LIKE 'pr_My_item_log_%')
AND (DATEDIFF(dd, getdate(), RIGHT(name, 8)) < -2)
There are procedures with these names in my database:
pr_My_app_log_20160830
pr_My_app_log_20160829
It should be something wrong with DATEDIFF. Can someone help me to correct the query and explain a little bit the issue?
Thank you.
When the last 8 characters of the object name cannot be parsed into a DATETIME, you are passing the DATEDIFF function a non-numeric value.
Try adding a check into your where clause:
SELECT name
FROM sysobjects
WHERE type = 'P'
AND (name LIKE 'pr_My_app_log_%' OR name LIKE 'pr_My_item_log_%')
AND TRY_CONVERT(DATETIME, RIGHT(name, 8)) IS NOT NULL -- check for non-numeric
AND (DATEDIFF(dd, GETDATE(), RIGHT(name, 8) ) < -2)

sql server get concatenated values of two diferrent column data types

Need your expert help in building sql query for concatenated values of two different column data types( first is date & second column is int)
select (LOCKER.AGRMNT_DATE + cast(LOCKER.AGRMNT_MTHS as varchar)) as AGRMNT_DUE_DATE from tableName
This is throwing an error:
Msg 402, Level 16, State 1, Procedure usp_sample, Line 52
The data types date and varchar are incompatible in the add operator.
select (CAST(LOCKER.AGRMNT_DATE AS VARCHAR) + cast(LOCKERREG.AGRMNT_MTHS as varchar)) as AGRMNT_DUE_DATE from tableName
Simple test:
DECLARE #testDate DATE = GETDATE();
DECLARE #testInt INT = 1;
SELECT CAST(#testDate AS VARCHAR) + CAST(#testInt AS VARCHAR);
Output:
2015-02-161

Convert String to Date in T-SQL

I try to convert a string into a date in t-sql. However get results that I can't explain.
DECLARE #String as char(11)
DECLARE #TString as char(11)
SELECT #String = SUBSTRING([Flat File Source Error Output Column],1,CHARINDEX(',',[Flat File Source Error Output Column])-6)
FROM [ERROR].[Import_V2X]
SELECT #TString = '12/18/2009'
-- Check content before conversion
SELECT #TString as 'CheckTString'
SELECT #String as 'CheckString'
-- Convert the strings to date
SELECT CONVERT(date,#TString,101) as 'ConvertSuccess'
SELECT CONVERT(date,#String,101) as 'ConvertFails'
[Flat File Source Error Output Column] is defined as text in the table
This gives me the following result:
CheckTString
------------
12/18/2009
(1 row(s) affected)
CheckString
-----------
12/18/2009
(1 row(s) affected)
ConvertSuccess
--------------
2009-12-18
(1 row(s) affected)
ConvertFails
------------
Msg 241, Level 16, State 1, Line 16
Conversion failed when converting date and/or time from character string.
Anybody can explain me where the problem is or comes from ?
For me the strings look exactly the same :(
By the look of your output you have a line feed in the checkstring variable. If this is not just a copy and paste error in the question, that will cause the error that you are describing. See below
DECLARE #TString as char(11)
SELECT #TString = '
12/18/2009'
-- Check content before conversion
SELECT #TString as 'CheckTString'
-- Convert the strings to date
SELECT CONVERT(date,#TString,101) as 'ConvertFails'
Gives the following results.
(1 row(s) affected)
Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.
If I had to guess it's because you're imported string has a non-visible character at the end of the string that doesn't allow it to convert. Your variable is char(11) but the string '12/18/2009' is only 10 characters long so that leaves room for 1 more character at the end.
Looks like #CheckString potentially has a newline character at the beginning.

Resources