Date conversion in SQL Server throws error - sql

I am trying to convert a date in mm/dd/yyyy format
select convert(date,'31/12/2013',101)
but I'm getting this error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
How to do this? My system (Windows 7) has a dd-mm-yyyy format.. will system date format will have any impact on it?`

see
http://msdn.microsoft.com/en-us/library/ms187928.aspx
101 is the US style (mm/dd/yyyy)
try 103 (dd/mm/yyyy) which is British/French
SELECT CONVERT(DATE, '31/12/2013', 103)

Style 101 is the US style, so this has months first - your string represents the 12th day of the 31st month ....
What you need to use is style 103 (British/French) which uses the day first - so this string is 31st of December:
SELECT CONVERT(DATE, '31/12/2013', 103)
See the official MSDN SQL Server Books Online Documentation on CAST and CONVERT and what styles are defined and what they mean

Related

convert stringformat 1 Jan 22 to datet 2022-01-01

I receive data from one of our partners with a column named "strip" (not sure of that matters) of type varchar(MAX) that contains dates in the form of: 01 Jan 22 and I need to change it to a date in SQL Server.
This works:
CONVERT(DATETIME, strip, 102) as Date
This also works:
CONVERT(DATETIME, strip, 106) as Date
However these options no longer work when I try to use this in the WHERE statement with the error:
Conversion failed when converting date and/or time from character string.
I'm stumped to be honest.

convert varchar to datetime field

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)

Datetime interpretation issue

I have the following data:
StartDate FinishDate Details
09/10/2013 11/10/2013 xxx
14/10/2013 13/10/2014 Taking a year off
Whilst editing this data I which to check the date ranges do not overlap.
I am running an SQL query from access via ado to do this; I am putting the dates entered into database format (ie 'mm/dd/yyyy'); This is the query I've got:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
('10/09/2013' BETWEEN StartDate AND FinishDate OR
'10/11/2013' BETWEEN StartDate AND FinishDate)
If the data is valid, it should return zero records; however it doesnt it returns 1 (being the second listed record above) and therefore seems to be interpreting '10/11/2013' as dd/mm/yyyy instead of mm/dd/yyyy.
Yet if I do this in SMO:
DECLARE #datevar datetime2 = '31/12/2008';
SELECT #datevar;
I get:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
While
DECLARE #datevar datetime2 = '12/31/2008';
SELECT #datevar;
returns
2008-12-31 00:00:00.0000000
So why am I having this problem and how do I fix it?
If you're running a query using MS Access, you need to delimit dates with # symbols, i.e.: #12/31/2008#. If this won't work for whatever reason, it is best to use string dates in the 'yyyy-mm-dd' format, as it will be recognized and is unambiguous.
You're probably getting this problem as MS is a US company, and the US uses mm/dd/yyyy format, so MS has defaulted much of their older software to treat dates as being in this format if at all possible, whereas you're probably in a country that uses - and have your PC's locality set to use - dd/mm/yyy format. Since not all of MS' software follows this rule, you have this problem.
The solution is to use a string date format that is unambiguous, such as: yyyy-mm-dd, mmm/dd/yyyy, or dd/mmm/yyyy (where mmm returns a three-letter month such as Dec).
You are using dd/mm/yyyy formats for your date strings. By default, without an explicit conversion, SQL is expecting date strings in the mm/dd/yyyy or yyyy-mm-dd format. So either change your strings to match one of these formats or do this:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
(CONVERT(DATETIME, '10/09/2013', 103) BETWEEN StartDate AND FinishDate OR
CONVERT(DATETIME, '10/11/2013', 103) BETWEEN StartDate AND FinishDate)

In SQL Server, convert 'mmmm dd,yyy' string to datetime format

In SQL Server 2008, how do I convert a string of type 'MMMM DD,YYYY' (i.e. June 01, 2013) to a regular datetime type (20130601)?
As far as I know, Convert() only works to convert dates into strings, not the other way around. I'm sure there's a standardized answer somewhere, but I can't find one for SQL Server. Should I write a custom procedure to parse the string?
While it's not recommended to store your date in string formats, you still could properly convert string to date. When you know exact format, avoid using cast and use convert - see possible formats here.
set language english
select convert(datetime, 'June 01, 2013', 107)
-----------------------
2013-06-01 00:00:00.000
Conversion from and to 107 format depends on your connection settings. If, for example, your connection language is not english, you could get an error
set language norwegian
select convert(datetime, 'June 01, 2013', 107)
-----------------------
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.

SELECT CONVERT(VARCHAR(10), GETDATE(), 110) what is the meaning of 110 here?

When we convert or cast date in sql, see below sql code
SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]
it works fine, I just want to know the meaning of 110 in above code. what it does actually, sometimes we use 102, 112 etc. what is the use of that number.
That number indicates Date and Time Styles
You need to look at CAST and CONVERT (Transact-SQL). Here you can find the meaning of all these Date and Time Styles.
Styles with century (e.g. 100, 101 etc) means year will come in yyyy format. While styles without century (e.g. 1,7,10) means year will come in yy format.
You can also refer to SQL Server Date Formats. Here you can find all date formats with examples.
110 is the Style value for the date format.
TSQL Date and Time Styles
When you convert expressions from one type to another, in many cases there will be a need within a stored procedure or other routine to convert data from a datetime type to a varchar type. The Convert function is used for such things. The CONVERT() function can be used to display date/time data in various formats.
Syntax
CONVERT(data_type(length), expression, style)
Style - style values for datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).
Example 1
take a style value 108 which defines the following format:
hh:mm:ss
Now use the above style in the following query:
select convert(varchar(20),GETDATE(),108)
Example 2
we use the style value 107 which defines the following format:
Mon dd, yy
Now use that style in the following query:
select convert(varchar(20),GETDATE(),107)
Similarly
style-106 for Day,Month,Year (26 Sep 2013)
style-6 for Day, Month, Year (26 Sep 13)
style-113 for Day,Month,Year, Timestamp (26 Sep 2013 14:11:53:300)
10 = mm-dd-yy
110 = mm-dd-yyyy
SQL Server CONVERT() Function