I want to convert from CRSE_EDTE (string) to datetime. Current CRSE_EDTE date format is YYYYMM, so I decided to put 28 as day for every date (DD).
select
try_convert(datetime, [CRSE_EDTE], 112) + CRSE_EDTE + '28' as new_CRSE_EDTE
FROM
[SMBM_DBPELJ].[DBPELJ].[MSTUMASTER]
I get an error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I don't have your database to play with, but would something like this work?
select try_convert(datetime,LEFT([CRSE_EDTE], 4)+'-'+RIGHT([CRSE_EDTE],2)+'-28',111) as new_CRSE_EDTE
FROM [SMBM_DBPELJ].[DBPELJ].[MSTUMASTER]
Related
In my SQL table, I have a column with just 'yyyy-mm', how can I covert into a Date object and compare if it is older than 2 months from now?
When I try this, CONVERT fails because the [Month] format is just `yyyy-mm' without the day
where DATEDIFF(day,CONVERT(DATE, [Month]),GETDATE()) < 60
I am using SQL Server.
I have tried the answer from the comment
SELECT
COUNT(*) AS aCount
FROM [MyTable]
where [name] != 'abc'
GROUP BY CONVERT(DATE, [Month] + '-01')
But I get error saying
Msg 8152, Level 16, State 2, Line 1
String or binary data would be truncated.
Does anyone know why something like this:
SELECT DATEADD(HOUR,4,'2021-11-01 05:10:00.0000000')
returns the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
But when using only up to milliseconds like
SELECT DATEADD(HOUR, 4, '2021-11-01 05:10:00.000')
it works and returns:
2021-11-01 13:10:00.000
And when using it in a table where the column includes the nanosecond like
SELECT DATEADD(HOUR, 4, column-name)
it also works.
Because you've supplied a literal string, and so DATEADD converts the value to it's default data type, datetime. A datetime is accurate to 1/300th of a second and a value like 2021-11-01 05:10:00.0000000 therefore is too large; it has an accuracy greater than 1/300th of a second.
If you explicitly converted the value to a date and time data type that goes up to 1/1000000 of a second, it would work:
SELECT DATEADD(HOUR,4,CONVERT(datetime2(7),'2021-11-01 05:10:00.0000000'))
From the dateadd documentation:
A string literal value must resolve to a datetime.
The datetime type can only accept milliseconds. Attempts to parse a string that has more accuracy than milliseconds will fail.
(Note that the datetime is rounded .000, .003, or .007 seconds. For example, 1 ms is rounded down to 0.)
The same error occurs if you try to declare a datetime and initialize it to a literal string that has more than 3 decimal places of second accuracy:
DECLARE #datetime datetime = '12:10:05.0000'
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
You can add to a datetime2, but you must declare the datetime2 variable first instead of using a string literal:
declare #date datetime2 = '2021-11-01 05:10:00.0000000'
SELECT DATEADD(HOUR, 4, #date)
2021-11-01 09:10:00.0000000
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
I have a sysdate() value - 2016.10.18. I need a SQL to convert it to int in MS SQL SERVER.
INPUT - 2016.10.18
OUTPUT reqd. - 20161018
I tried this -
SELECT CONVERT(int, '2016.10.18')
but it throws the following error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2016.10.18' to data type int.
Also tried this -
select CONVERT(int,CONVERT(decimal(19,2),'2016.10.18'))
Getting the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Could someone help me out!?
Select Convert(int,Convert(varchar,cast('2016.10.18' as date),112))
Returns
20161018
Either of the following will work. If you have a use the replace() method, you will need to strip the time portion off via LEFT(...,10)
Select Convert(int,Convert(varchar,cast('2016.10.18 14:24:49' as date),112))
Select cast(Replace(Left('2016.10.18 14:24:49',10),'.','') as int)
Updated answer.
Select Cast(Convert(varchar(8), Cast(Left('2016.10.18 14:24:49',10) as Date), 112) as int)
select
DATEDIFF(SECOND, '1970-01-01 ',
concat((select concat((CONVERT(varchar,getdate(),102)),' ','12:00:01 AM')),'%m/%d/%Y %h:%i:%s%p'))
Having problem while using concat in SQL Server. Getting error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
SELECT DATEDIFF(SECOND, '1970-01-01 ',
(SELECT CONCAT(CONVERT(varchar, GETDATE(), 102),' ','12:00:01 AM')))
You don't need the first concat statement and the datetime formatting at the end