There is too much precision in the varchar to be converted into datetime.
One option(better in my opinion) would be to change the target column to datetime2(7). Then you can convert like this:
declare @dt varchar(50)
set @dt = '2015-12-02 20:40:37.8130000'
select cast(@dt as datetime2(7));
If changing the column is not an option the you can do the conversion like this:
declare @dt varchar(50)
set @dt = '2015-12-02 20:40:37.8130000'
select cast(cast(@dt as datetime2(7))as datetime)
Answer from Bob Klimes on Stack ExchangeThere is too much precision in the varchar to be converted into datetime.
One option(better in my opinion) would be to change the target column to datetime2(7). Then you can convert like this:
declare @dt varchar(50)
set @dt = '2015-12-02 20:40:37.8130000'
select cast(@dt as datetime2(7));
If changing the column is not an option the you can do the conversion like this:
declare @dt varchar(50)
set @dt = '2015-12-02 20:40:37.8130000'
select cast(cast(@dt as datetime2(7))as datetime)
DATETIME only allows three places of millisecond precision. You'll either need to trim the trailing places beyond the first three milliseconds or if you're using any version of SQL Server 2008 or later you can use DATETIME2.
DATETIME2 allows up to seven places of millisecond precision and the precision level can be explicitly set for your table or query. Further details and differences can be found at datetime2 (Transact-SQL).
You can verify by executing the following CAST query:
SELECT CAST('2015-12-02 20:40:37.8130000' AS DATETIME2)
Convert varchar into datetime in SQL Server - Stack Overflow
SQL | Convert varchar to date in WHERE clause
SQL - Convert nvarchar to datetime "MM/DD/YYYY" format
Converting varchar to datetime – SQLServerCentral Forums
Videos
Hi guys, SQL novice here - I was hoping for a little help. I have this Pivot table query from SQhint which is exactly what I need . Unfortunately, my column for the date source is varchar type and not date time, however the data is formatted as YYYYMMDD (eg. 20200120). Google says I can use CASE or CONVERT but I am not sure exactly how to implement it with this query. Any help is greatly appreciated.
https://sqlhints.com/tag/sql-group-by-month-and-year/
SELECT *
FROM (SELECT YEAR(SalesDate) [Year],
DATENAME(MONTH, SalesDate) [Month],
COUNT(1) [Sales Count]
FROM #Sales
GROUP BY YEAR(SalesDate),
DATENAME(MONTH, SalesDate)) AS MontlySalesData
PIVOT( SUM([Sales Count])
FOR Month IN ([January],[February],[March],[April],[May],
[June],[July],[August],[September],[October],[November],
[December])) AS MNamePivotOP wants mmddyy and a plain convert will not work for that:
select convert(datetime,'12312009')
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
so try this:
DECLARE @Date char(8)
set @Date='12312009'
SELECT CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,3,2))
OUTPUT:
-----------------------
2009-12-31 00:00:00.000
(1 row(s) affected)
SQL Server can implicitly cast strings in the form of YYYYMMDD to a datetime - all other strings must be explicitly cast.
Here are two quick code blocks which will do the conversion from the form you are talking about (MMDDYYYY).
Version 1 uses unit variables:
DECLARE @input VARCHAR(8)
, @mon CHAR(2)
, @day char(2)
, @year char(4)
, @output DATETIME
SET @input = '10022009' --today's date
SELECT @mon = LEFT(@input, 2)
, @day = SUBSTRING(@input, 3,2)
, @year = RIGHT(@input,4)
SET @output = @year + @mon + @day
SELECT @output
Version 2 does not use unit variables:
DECLARE @input CHAR(8)
, @output DATETIME
SET @input = '10022009' --today's date
SELECT @output = RIGHT(@input,4)
+ SUBSTRING(@input, 3,2)
+ LEFT(@input, 2)
SELECT @output
Both cases rely on SQL Server's ability to do that implicit conversion.