Convert character string into this specific date format? - sql

I am using SQL Server 2014 and I have a table (t1) which contain a column (ReviewDate) in the nvarchar format.
An example of a row of this column is given below:
ReviewDate
Mr John wrote a review in Oct 2017
I need to extract the "date" component from this character string.
To do this, my T-SQL is as follows:
SELECT (RIGHT([ReviewDate], 8)) as [ReviewDate 2]
FROM t1
This gives me "Oct 2017".
Now, I want to convert the "Oct 2017" into "2017-10-01" as a datetime format. This is where I am stuck.
I have tried the following:
SELECT CONVERT(datetime, (RIGHT([ReviewDate], 8)), 121) as [ReviewDate2]
Above syntax gives me the following error message: "Conversion failed when converting date and/or time from character string."
SELECT CAST( (RIGHT([ReviewDate], 8)) as datetime) as [ReviewDate2]
Above syntax gives me the same error message:
Conversion failed when converting date and/or time from character string.
Some help will be appreciated.

All your queries are right but make sure that, it should not have any other string apart from date part.
For example SELECT CAST('x Oct 2017' AS DATE) will give you error like
Conversion failed when converting date and/or time from character
string.
SELECT CAST((RIGHT('Mr John wrote a review in Oct 2017', 8)) as datetime) as [ReviewDate2]
SELECT CAST('Oct 2017' AS DATE)
SELECT CONVERT(DATETIME, 'Oct 2017 ', 121) as [ReviewDate2]
FIDDLE DEMO

so far your sample text is a valid datetime in mssql when I tried to cast. It seems there's some invalid data on your table. try using try_cast() to include those invalid data.
declare #ReviewDate varchar(max)='Mr John wrote a review in Oct 2017'
set #ReviewDate = (RIGHT(#ReviewDate, 8))
select try_cast(#ReviewDate as datetime) as [ReviewDate2]
dbfiddle<>

Related

Converting date to YYYYMMDD format in SQL Server 2016 from T format

I have a date column in a SQL table where the values are stored in the below format.
2020-04-07T02:41:49
I need to get the date in YYYYMMDD format, like this: 20200407.
I tried using the below statements but it throws an error on conversion.
SELECT
CONVERT(DATETIME, SUBSTRING(CAST('2020-04-07T02:41:49' AS varchar), 1, 8))
SELECT
LEFT(4, CAST('2020-04-05T08:05:47' AS nvarchar))
Error:
Conversion failed when converting date and/or time from character string
Could someone please help?
Thanks.
You can use try_convert(date,...)
Example
Select WithFormat = format(try_convert(date,'2020-04-07T02:41:49'),'yyyyMMdd')
,OrNot = convert(varchar(10),try_convert(date,'2020-04-07T02:41:49'),112)
,OrString = replace(left('2020-04-07T02:41:49',10),'-','')
Returns
WithFormat OrNot OrString
20200407 20200407 20200407

SQL: convert string to datetime in Sybase IQ

I am trying to convert a string into a datetime in Sybase IQ. The string has this form: '20191211 11:49:00.565224 +0700'
I first try with Convert DATETIME:
the following 2 queries fail:
SELECT CONVERT("DATETIME", '20191211 16:29:56.226560 +0000', 112) AS "TIME_dt"
SELECT CONVERT(DATETIME, '20191211 16:29:56.226560 +0000', 112) AS "TIME_dt"
However, when used in a query like
SELECT CONVERT("DATETIME", "TIME_", 112) AS "TIME_dt" FROM TABLE
and the column "TIME_" contains '20191211 16:29:56.226560 +0000' then I get the result: 2019-12-11 00:00:00 and it correctly transformed 20191211 to 2019-12-11
Next I try with SELECT CONVERT( DATETIMEOFFSET , '20191211 11:49:00.565224 +0700') and that works, but when I use it in a query like
SELECT CONVERT(DATETIMEOFFSET, "TIME_") AS "TIME_dt" FROM TABLE
I get an error with :
[Code: 21, SQL State: QFA2A] SQL Anywhere Error -1001030: Feature, Cast to Unknown TypeID (29) at line 1, is not supported.
-- (dflib/df_Heap.cxx 6835)
What is going on?
thanks
I had a look at https://www.w3schools.com/sql/func_sqlserver_convert.asp, basically, the third parameter is optional, in your case 112, it tells you exactly the format, that's why you are getting the formatting error, because it's missing 112. The other error has to do with how to pass the variable, what you are actually passing, a string or a date value.

Convert varchar containing various formats to DATETIME

I have a record_created column of type varchar containing multiple values formatted in two different ways throughout.
2017-04-17 16:55:53.3840460
Sep 18 2015 11:25PM
How can I convert this column into a DATETIME to be compared to GETDATE?
GETDATE() is SQL Server specific if so, then you can use try_convert() :
select cast(try_convert(datetime2, col) as datetime)
from table t
where try_convert(datetime2, col) is not null;
However, if the string date is exactly the same format which you have provide then you can simply do casting :
select cast(cast(col as datetime2) as datetime)
from table t;
If you are using SQL Server, then you may be able to use the CONVERT function here:
SELECT
CONVERT(datetime, LEFT('2017-04-17 16:55:53.3840460', 23), 121) AS date1,
CONVERT(datetime, 'Sep 18 2015 11:25PM', 100) AS date2;
Your first type of timestamp seems to work with mask 121, and the second one works with mask 100. The demo link below shows that the conversions are working.
Demo

Conversion issues with VARCHAR to DATE

I have sample data like this :
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/1997', 3), 120);
when i execute this I'm getting Error like this :
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character
string.
So then i have removed first 2 characters of year 1997
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/97', 3), 120);
And it gave Result Set like this :
Date
1997-10-11
So it got converted from DD/MM/YY Format to YYYY-MM-DD
Fine But why it has taken 1997 if I'm going to give 17 it will give as 2017.
If i give 37 it will give 2037 and if i give 67 and it is giving 1967 .
And lastly how can DD/MM/YYYY to YYYY-MM-DD Format
You need to use 103 when you specify four digit year:
SELECT CONVERT(DATE, '11/10/1997', 103)
-- 1997-10-11 (DATE)
If you want to convert the date back to a string, convert it again:
SELECT CONVERT(VARCHAR(10), CONVERT(DATE, '11/10/1997', 103), 120)
-- 1997-10-11 (VARCHAR)
Complete list of styles is available here.
Instead of 3 you can pun 103.
So :
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/1997', 103), 120); will return 1997-10-11
Can you try this?
declare #datechar as char(19) ='11/10/1997'
select FORMAT(convert(date,#datechar,103),'yyyy-MM-dd')

SQL Server : convert string to numeric - Conversion failed when converting date and/or time from character string

I got a dataset in SQL Server Management Studio.
The data looks like the following
month Year
-------------
Feb 2016
Jan 2015
I want to create a numeric date using 01 as dd. I tried
CAST('01-'+ month +'-'+ year AS DATE)
also
CONVERT(DATETIME, '01-'+ month +'-'+ year, 106) AS
Both of them cause this error:
Conversion failed when converting date and/or time from character string.
Can anyone teach me how to covert the string into numeric datetime/date please.
Perhaps the reason is the hyphens. You should try with spaces instead:
convert(datetime, '01 '+ month +' '+ year, 106)
Another possibility is that month contains invalid values. In SQL Server 2012+, use try_convert() to avoid that problem:
try_convert(datetime, '01 '+ month +' '+ year, 106)
Another option is to use concat with convert if 2012+
Select convert(datetime, concat(1,month ,year), 106)
Use this code
convert(datetime, concat('01-', mnth , '-',yr), 106) AS d