SQL: convert string to datetime in Sybase IQ - sql

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.

Related

SQL Server 2017 Convert Varchar to Date

I have a date stored as nvarchar which I cannot seem to convert to a date.
I have tried the following but get the error message
Conversion failed when converting date and/or time from character string
select cast([month-year] as date) from [Reporting].[AIM].[Hires_PBI]
select convert(date, [month-year], 103)from [Reporting].[AIM].[Hires_PBI]
Any ideas here?
Find the values that are causing the problem using try_convert():
select [month-year]
from [Reporting].[AIM].[Hires_PBI]
where try_convert(date, [month-year], 103) is null and
[month-year] is not null;
Once you see what values are causing the problem, you can adjust the conversion logic to convert all values. Or just use try_convert() to get NULL for the non-convertible values.

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

Convert character string into this specific date format?

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<>

Convert a date in nvarchar to date format in a table on the SQL Server

Here is what I have done:
select top 1000 convert(nvarchar(50), cast([date written] as datetime), 1)
from practicetable
where convert(nvarchar(50), cast([date written] as datetime), 1) = '09/07/16'
Although this query works on a small scale anytime I try to select more than 1000 records the query fails and I get this error:
Conversion failed when converting datetime from character string
Why can't I select more than 1,000 records?
Always risky to save dates as a string. Since you don't have Try_Convert() in 2008, I would recommend that you
select distinct [date written] from practicetable order by 1
to see which dates are not formatted correctly, and scrub as necessary
The query is failing because it is trying to convert a string to a date, but the string does not fit the date format. In other words, the 'Conversion failed when converting datetime from character string'. You should take a closer look at the underlying data to determine which record is causing the issue.

Converting a numeric value to date time

I am working in SQL Server 2012. My date column in a data set looks like this: 41547. The column is in nvarchar (255). I want to convert it to something like this: yyyy-mm-dd hh:mm:ss (Example: 2013-09-14 12:23:23.98933090). But I can not do this. I am using following code:
select convert(datetime, date_column, 6)
But this is giving following error:
Msg 241, Level 16, State 1, Line 1 Conversion failed when converting
date and/or time from character string.
What am I doing wrong?
Your date is actually a numeric value (float or integer), stored in a char column. So, you need to convert it to a numerical value (in this case, to float) first, like:
select convert(datetime, CONVERT(float,date_column))
A value of 41547.5 will result in:
`2013-10-02 12:00:00`
The style argument, in your case 6 is only necessary when converting from or to char-types. In this case it is not needed and will be ignored.
NB: The float value is the number of days since 1900-01-01.
e.g. select convert(datetime, CONVERT(float,9.0)) => 1900-01-10 00:00:00; the same as select dateadd(day,9.0,'1900-01-01') would.
The decimal part of the number also equates to days; so 0.5 is half a day / 12 hours.
e.g. select convert(datetime, CONVERT(float,.5)) => 1900-01-01 12:00:00. (Here our comparison to dateadd doesn't make sense, since that only deals with integers rather than floats).
There is an easier way to do it as well.
select convert(date,cast (date_Column+ 19000000 as nvarchar(10)))
as date_Column_Formated
from table_Name
I have just found the way to do this.
First I have to covert the nvarchar to int then I have to convert it to date time. I have used following code:
Select convert(datetime, (convert (int, [date_column])), 6) as 'convertedDateTime' from mytable
6 format is: "dd mon yy"
Like this: SELECT convert(datetime, '23 OCT 16', 6)
Other formats will cause your error
SELECT CONVERT(DATETIME,CONVERT(INT,date_column))