convert varchar to datetime field - sql

I'm trying to filter some records by date but the datetime field seems to be stored as varchar and I'm struggling to convert it. Below is a sample of the data:
ID DateField
0002 14/04/1989 01:30
0003 16/04/1989 09:45
0004 16/04/1989 06:00
0005 19/04/1989 01:07
0006 21/04/1989 16:03
When I use
cast(Datefield as datetime)
I get the following error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Edit - this does actually display results in datetime format but with the error message, why is that?
What's the best way to convert my datefield to valid datetime format? Thanks

Use CONVERT with Style
CONVERT(DATETIME,Datefield ,103)
To find bad data, see what this returns
set dateformat dmy
select Datefield from table where isdate(where)=0
You should always use proper DATETIME datatype to store datetime values

Try this. You need to add style part to identify the format
SELECT CONVERT(DATETIME, dates, 103)
FROM (VALUES ('14/04/1989 01:30'),
('16/04/1989 09:45'),
('16/04/1989 06:00'),
('19/04/1989 01:07'),
('21/04/1989 16:03')) cs (dates)

see CAST and CONVERT
you need to use
select CONVERT(datetime,your_Datefield ,103)
because your your_Datefield holding the datetime value in dd/mm/yy format.
follow the link, you'll get the explanation.

You can get the error because of by default datetime format is mm/dd/yyyy. So If your value have this format, then it easily convert without error.
For this you have to convert with format which convert the string into proper date-time format. For this cast will not work , but convert work. Here is link for more details.
As your format is dd/mm/yyyy, you need to use 103 format. Just check this to understand.
Select convert( datetime, getdate(), 103)

Related

CASTing and converting functions in SQL Server

I am trying to convert dd.mm.yyyy to yyyy-mm-dd.
select convert(date,CAST(WEEK_DATE as nvarchar(220)), 120)
from z_fact
Error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
How can I resolve this?
Since your date is actually text, you must first convert it to a bona fide date using CONVERT. Then, use CONVERT on that date a second time to generate the text output you want.
SELECT CONVERT(varchar(20), CONVERT(datetime, '15.03.18', 4), 120);
Demo
Note that it is generally bad practice to store your dates as text. Hopefully you can use my answer to tidy up your table. For example, you could add a new datetime column new_dt and then update it using:
UPDATE yourTable
SET new_dt = CONVERT(datetime, old_dt, 4);
Don't worry about the internal format used by SQL Server. If you still need to display yyyy-mm-dd output, then use CONVERT again, as I did in my first query.
You can try this:
declare #dt NVARCHAR(12) = '15.03.18'
SELECT CONVERT(DATE,#dt,3)
GO

How to convert date time format in SQL Server like '2017-03-04 10:07:03.490' to date format which is seperated by - like '2-11-2016'

I am tying to make date comparison with the query
select *
from dbo.OH_Case
where dbo.OH_Case.CreatedDate between Convert(varchar(30),'24/04/2017', 102)
and Convert(varchar(30),'01/05/2017', 102)
but it throws this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
My application passes data in '01/05/2017' format and the date information in SQL Server is stored in '2017-03-04 10:07:03.490' format. What should I do to make both of these in same format which allows comparison?
First, you're converting a string literal to a varchar. Convert it to a datetime instead.
Second, you're passing in 24/04/2017, but date format 102 is yyyy.mm.dd. Try date format 103 for dd/mm/yyyy:
convert(datetime, '24/04/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter.
convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between cast('24/04/2017' as datetime) and
cast('01/05/2017' as datetime)
Updates:
Siddharth, I have checked you date values and figured out that issue is in date "24/04/2017". This format is for "MM/dd/yyyy". and 24 is passed as month which is not valid. Therefore it is throwing exception. I have updated result
to handle such exceptions.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between convert(datetime, '24/04/2017', 103) and
convert(datetime, '01/05/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter. convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from dbo.OH_Case format(where dbo.OH_Case.CreatedDate, 'dd/mm/yyyy') as CreatedDate

Why does SQL Server convert VARCHAR to DATETIME using an invalid style?

I can't make out from the documentation why SQL Server parses a text in a format other than the specified style.
Regardless of whether I provide text in the expected format:
SELECT CONVERT(DATETIME, N'20150601', 112)
or incorrect format (for style 113):
SELECT CONVERT(DATETIME, N'20150601', 113)
The results are the same: 2015-06-01 00:00:00.000 I would expect the latter to fail to convert the date (correctly).
What rules does it employ when trying to convert a VARCHAR to DATETIME? I.e. why does the latter (incorrect format style) still correctly parse the date?
EDIT: It seems I've not been clear enough. Style 113 should expect dd mon yyyy hh:mi:ss:mmm(24h) but it happily converts values in the format yyyymmdd for some reason.
Because the date is in a canonical format ie(20150101). The database engine falls over it implicitly. This is a compatibility feature.
If you swapped these around to UK or US date formats, you would receive conversion errors, because they cannot be implicitly converted.
EDIT: You could actually tell it to convert it to a pig, and it would still implicitly convert it to date time:
select convert(datetime,'20150425',99999999)
select convert(datetime,'20150425',100)
select convert(datetime,'20150425',113)
select convert(datetime,'20150425',010)
select convert(datetime,'20150425',8008135)
select convert(datetime,'20150425',000)
And proof of concept that this is a compatibility feature:
select convert(datetime2,'20150425',99999999)
Although you can still implicitly convert datetime2 objects, but the style must be in the scope of the conversion chart.
Reason why is the date N'20150601' converted to valid datetime is because of fact that literal N'20150601' is universal notation of datetime in SQL Server. That means, if you state datetime value in format N'yyyymmdd', SQL Server know that it is universal datetime format and know how to read it, in which order.
You should convert to varchar type in order to apply those formats:
SELECT CONVERT(varchar(100), CAST('20150601' as date), 113)
OK, you are converting datetime to datetime. What did you expect? In order to apply formats you should convert to varchar and you have to have date or time type as second parameter.

Converting to mm/dd/yyyy format

I have a table called SF_Data and there is a column called IN_Date, ID the data looks like:
ID IN_Date
1 9/8/2010
2 26/04/2011
3 20/09/2010
The datatatype of IN_Date is varchar(50).
I am trying to convert the IN_Date to mm/dd/yyyy format. I tried doing this:
Select convert(varchar,IN_Date,103) From dbo.SF_Data
But still the format doesn't change. Can anyone tell me where I am going wrong
You need a convert to fix the data (to the correct datatype) before formatting...
Select
convert(varchar,
convert(date, IN_Date, 103),
101)
from dbo.SF_Data
The 3rd parameter to convert has no meaning when converting from varchar to varchar. So per #marc_s' comment, you'd have to convert the varchar to a datetime using the 103 format, and then from datetime to varchar specifying the 101 format:
Select convert(varchar(12),convert(datetime,IN_Date,103),101) From dbo.SF_Data
For example:
select convert(varchar(12),convert(datetime,'31/12/2001',103),101)
prints 12/31/2001.
See MSDN.

sql datetime formats conversion

How can i convert a datetime of type dd-mm-yyyy to dd/mm/yyyy in sql?
I dont want to convert to varchar but to datetime.
Can anyone give me an idea please?
A DateTime is a DateTime is a DateTime - it doesn't have " a format" - it's just a DateTime.
The format or display options only come into play when you want to display the DateTime as a string.
of type dd-mm-yyyy to dd/mm/yyyy
To me, this sounds as if:
you have string representing a date, in the format dd-mm-yyyy
you want to convert that to a DateTime and then again display a string representing that DateTime in the dd/mm/yyyy format
Right??
Check out the MSDN SQL Server Books Online for CAST and CONVERT - it lists all possible, valid, supported string formats representing a date and time.
Format dd-mm-yyyy is style no. 105, so this will convert your first string:
SET #YourDateTime = CONVERT(DATETIME, '23-11-2010', 105)
Now you have your DATETIME value (Nov 23, 2010) in #YourDateTime - just a DateTime, no formatting applied.
If you want to display that date value in format dd/mm/yyyy, which is style no. 103, use this snippet of T-SQL to achieve this:
SELECT CONVERT(VARCHAR(50), #YourDateTime, 103)
This will display 23/11/2010.