SQL Server : varchar to Date Conversion/Casting Error
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101
is for US date and 103
is for UK/French Date.
sql-server tsql
add a comment |
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101
is for US date and 103
is for UK/French Date.
sql-server tsql
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52
add a comment |
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101
is for US date and 103
is for UK/French Date.
sql-server tsql
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101
is for US date and 103
is for UK/French Date.
sql-server tsql
sql-server tsql
edited Apr 4 '14 at 10:33
marc_s
571k12811031251
571k12811031251
asked Apr 4 '14 at 10:28
maitiest
74311
74311
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52
add a comment |
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52
add a comment |
4 Answers
4
active
oldest
votes
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy
) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy
): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT
here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD
or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
add a comment |
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
add a comment |
The convert signature is as followsCONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
add a comment |
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22860229%2fsql-server-varchar-to-date-conversion-casting-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy
) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy
): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT
here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD
or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
add a comment |
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy
) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy
): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT
here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD
or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
add a comment |
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy
) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy
): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT
here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD
or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy
) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy
): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT
here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD
or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
answered Apr 4 '14 at 10:35
marc_s
571k12811031251
571k12811031251
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
add a comment |
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
so would it always assume the format 'YYYYMMDD'?
– Tauseef
Apr 4 '14 at 10:39
1
1
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
@Tauseef: yes - that's what the ISO-8601 format defines
– marc_s
Apr 4 '14 at 10:40
add a comment |
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
add a comment |
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
add a comment |
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
edited Apr 4 '14 at 10:57
Tauseef
1,5331215
1,5331215
answered Apr 4 '14 at 10:34
Rahul Tripathi
126k21162233
126k21162233
add a comment |
add a comment |
The convert signature is as followsCONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
add a comment |
The convert signature is as followsCONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
add a comment |
The convert signature is as followsCONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
The convert signature is as followsCONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
answered Apr 4 '14 at 10:36
Tauseef
1,5331215
1,5331215
add a comment |
add a comment |
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
add a comment |
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
add a comment |
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
edited Nov 11 '18 at 21:21
Aaron Bertrand
207k27362404
207k27362404
answered Apr 4 '14 at 10:39
dean
8,26911319
8,26911319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22860229%2fsql-server-varchar-to-date-conversion-casting-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35
@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52