In a table called mt, how do I convert the Sun column to date format from nvarchar(50)?
Sun
------------------------
Sunday, March 24, 2019
I tried the below query but it errored out.
SELECT CONVERT(DATETIME,Sun,109) FROM [mt]
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
I think this will work:
select convert(date, stuff(sun, 1, charindex(',', Sun) + 1, ''))
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.
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')
I want to convert the julian date in a date format.
Below, please find an example to create my error. All my dates have 5 digits I need to convert.
create table #test (dateR int)
insert into #test (dateR)
values (39596),(39596),(39595),(39595),(39593),(39592),(39592),(39589),(38104),(38104),(37957)
SELECT * from #test
select *
, dateadd (year, dateR/1000 - 1900, dateR %1000 - 1) as Rd
from #test
Getting the error:
Msg 517, Level 16, State 1, Line 2 Adding a value to a 'datetime'
column caused an overflow.
Also tried:
SELECT DATEADD(dd, CONVERT(int, RIGHT(dateR,3)) - 1, CONVERT(datetime,SUBSTRING(dateR,1,2)+'0101', 212))
This returns:
Msg 8116 level 16 state 1 line 1 nullArgument data type int is invalid for argument of substring function
You can just cast the INT value to a DATETIME.
For example:
SELECT CAST(39596 AS DATETIME); -- Returns 2008-05-30 00:00:00.000
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]
In SQL Server 2005, why does:
PRINT Cast('' AS datetime)
display:
Jan 1 1900 12:00AM
I would have thought it should be null?
It's because empty string '' is not NULL. If you do:
select Cast(null AS datetime)
OUTPUT:
-----------------------
NULL
(1 row(s) affected)
CAST and CONVERT (Transact-SQL)
When character data that represents
only date or only time components is
cast to the datetime or smalldatetime
data types, the unspecified time
component is set to 00:00:00.000, and
the unspecified date component is set
to 1900-01-01.
The empty string is casted to 0 which is later casted to the era date.
Unlike Oracle, SQL Server distinguishes between NULL and an empty string.
From experimentation, it looks like SQL Server attempts to cast directly to DateTime, and failing that attempts to cast to int and then to DateTime:
PRINT Cast('2009-1-1' AS datetime)
go
PRINT Cast('2009/1/1' AS datetime)
go
PRINT Cast('1.1' AS datetime)
go
PRINT Cast('1/2009/1' AS datetime)
go
PRINT Cast('' AS int)
go
PRINT Cast(' ' AS int)
go
PRINT Cast(0 AS datetime)
go
PRINT Cast('X' AS datetime)
go
PRINT Cast('X' AS int)
Output:
Jan 1 2009 12:00AM
Jan 1 2009 12:00AM
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Jan 1 2009 12:00AM
0
0
Jan 1 1900 12:00AM
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'X' to data type int.