cast in sql server yyyy/mm/dd instead of dd/mm/yyyy - sql-server

I have a script to insert 1000 rows of data in a table, in the datetime column I have this information.
CAST(N'2015-05-14 00:00:00.000' AS DateTime)
The problem is cast is trying to cast in dd/mm/yyyy format where the input is in yyyy/mm/dd format.

Don't use cast when converting varchar to datetime, use convert instead, as convert takes in a date format.
convert(datetime, '2015-05-14 00:00:00.000', 121)
See more at:
http://www.sqlusa.com/bestpractices/datetimeconversion/
its already written that way since i have a generated script of data to insert, so i cant change one by one, too many rows to insert
Are you unable to modify the script? This seems like an easy find/replace.
CAST(N' -> convert(datetime, '
' AS datetime) -> ', 121)

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

How to convert from DD/mm/yyyy in varchar(max) to yyyymmdd format in SQL Server

Dates in DD/mm/yyyy format stored in a varchar(max) column need to be converted to yyyymmdd format.
The below options to convert / cast are not supported.
CONVERT(VARCHAR(19), CONVERT(DATETIME, wfm.date, 3), 112)
CONVERT(VARCHAR(50), CAST(wfm.date AS DATETIME2), 112)
CAST(CAST(wfm.date AS DATETIME2(15)) AS DATETIME)
I'm getting an error message for CONVERT syntax:
Conversion failed when converting date and/or time from character string.
For CAST syntax, I'm getting this error:
Line 1: Specified scale 15 is invalid. 'dd/mm/yyyy/ in varchar to be converted to yyyymmdd format
Ideally, don't store dates as a varchar, store them as a date and worry about the format they are displayed in in your presentation layer. Therefore, you should just do:
SELECT TRY_CONVERT(date,YourVarcharMAXDate,103);
If you must get the format yyyyMM`` (i don't recommend, change your data type of your column todate), then you can use a furterhCONVERT`:
SELECT CONVERT(varchar(8),TRY_CONVERT(date,YourVarcharMAXDate,103),112);
If the data engine can't convert the date, due to it being an invalid format, TRY_CONVERT will return NULL; you'll need to fix those values (make them valid). Another good reason why not to use a varchar to store a date.
Maybe this helps
DECLARE #dt DATETIME= '2019-12-31 14:43:35.863';
SELECT CONVERT(VARCHAR(10), #dt, 11) s1,
CONVERT(VARCHAR(10), #dt, 111) s2;
You could always just use substring for this. Example is shown below.
DECLARE #samples TABLE
(
Dates NVARCHAR(50)
)
INSERT INTO #samples VALUES
('10/11/2018'),
('12/11/2018'),
('15/11/2018'),
('17/11/2018'),
('19/11/2018'),
('20/11/2018')
SELECT
SUBSTRING(Dates, 7,4) +
SUBSTRING(Dates, 4,2) +
SUBSTRING(Dates, 1,2) as [NewDate]
FROM #samples
Output will be a list of strings with your required format. This won't validate for you, but you can always try convert these to a date using CAST and it should correctly cast the yyyyMMdd to a date if the date is correct.
If you're using SQL Server 2016 or newer, you can use PARSE and FORMAT to do this quite easily:
SELECT
FORMAT(PARSE(wfm.Date AS DATE USING 'de-DE'), 'yyyyMMdd')
FROM
dbo.WhateverYourTableNameIS
The dd/mm/yyyy format is the format used e.g. in Germany, that's why I'm using the de-DE culture to convert that string to a DATE. Then you can format it using the usual formatting strings as used in C#/.NET.
select convert(varchar, convert(datetime, wfm.date, 103), 112)

How to convert integer into datetime in varchar format

I m getting this error when trying to convert below integer into datetime in a varchar format.
Conversion failed when converting date and/or time from character string
Code:
CONVERT(VARCHAR(MAX), CAST('200901041421' AS DATETIME))
You need a space, and a colon. I'm not sure why you want to do this though... present the date in a format on your front end (presentation layer) and keep dates stored as dates or datetime in the database and you won't run into this issue :)
SELECT CONVERT(VARCHAR(MAX), CAST('20090104 14:21' AS DATETIME))
Also, no need to use MAX here. That's a waste of storage. Something like this makes more sense.
SELECT CONVERT(VARCHAR(24), CAST('20090104 14:21' AS DATETIME), 113)
Using a column name...
SELECT CONVERT(VARCHAR(24), CAST(YourColumnName AS DATETIME), 113)
FROM YourTable
You can see other conversions here

can't find the date style format in sql server 2012

I want to convert this nvarchar column to Datetime format:
"29/12/14 07:46:20 PM"
This style is good but has no time:
SELECT CONVERT(datetime, nvarcharDateColumn, 3) AS [DD/MM/YY]
How can i convert it and keep all the date parts (date+time) ??
A datetime data type in SQL Server has no "format". It is natively a 8-byte binary value. To convert the nvarchar string to datetime, use CONVERT with the nvarchar column as the second parameter:
SELECT CONVERT(datetime, nvarcharColumnWithDateAndTime, 3) AS NativeDateTime
FROM dbo.YourTable;
Note the datetime format displayed by this query is determined by the application your are using, not the SQL Server database. Also, consider changing the table data type to datetime to avoid data integrity issues; nvarchar will allow storing invalid dates.
This one has time, however includes Miliseconds...it could be too much :)
SELECT CONVERT(VARCHAR(30), GETDATE(), 131)
This is a harder task that I would have thought. This is the best I could come up with:
DECLARE #MyDate DATETIME
SET #MyDate = GETDATE()
SELECT CONVERT(VARCHAR(50), #MyDate, 3) + ' ' + CONVERT(VARCHAR(50), CONVERT(TIME, #MyDate), 100)

Need help on DateTime Conversion in Sql Server 2005

I am facing problem in DateTime converstion.
My input is 09/22/2011, I need to convert into 20110922.
I tried the below one, but failed.
Select CONVERT(VARCHAR(8),'09/22/2011' , 112) as DateConv
Here you go:
SET DATEFORMAT mdy
SELECT CONVERT(VARCHAR(8), CAST('09/22/2011' as DATETIME) , 112) AS DateConv
If your input is actually a dateTime variable like you said (but didn't show in your code example), you can simplify this down to:
SELECT CONVERT(VARCHAR(8), #myDate , 112) AS DateConv
This will do the trick:
select year('09/22/2011') * 10000 + month('09/22/2011') * 100 + day('09/22/2011')
Without any other information, SQL is interpreting the '09/22/2011' as a varchar already, and just passes the data through (ignoring CONVERT's style argument). If you use the following line:
SELECT CONVERT (VARCHAR (8), CAST ('09/22/2011' as DATETIME), 112) as DateConv
it should work as expected since it will then view the '09/22/2011' as a date value. If you were getting the date from a column in a table, it would already know the type and you will not need to worry about the CAST portion of the expression, just use the column name.

Resources