SQL server date convertion
up vote
2
down vote
favorite
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
sql
add a comment |
up vote
2
down vote
favorite
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
sql
1
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
If you use default date format that won't happen:2018-08-14
– juergen d
2 days ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
sql
I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
sql
sql
edited 2 days ago
Yogesh Sharma
26.6k51334
26.6k51334
asked 2 days ago
Tshepiso Selepe
114
114
1
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
If you use default date format that won't happen:2018-08-14
– juergen d
2 days ago
add a comment |
1
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
If you use default date format that won't happen:2018-08-14
– juergen d
2 days ago
1
1
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
If you use default date format that won't happen:
2018-08-14– juergen d
2 days ago
If you use default date format that won't happen:
2018-08-14– juergen d
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
add a comment |
up vote
1
down vote
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
add a comment |
up vote
2
down vote
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
edited 2 days ago
answered 2 days ago
Yogesh Sharma
26.6k51334
26.6k51334
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
add a comment |
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
Thanks a lot this has solved my issue.
– Tshepiso Selepe
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
@TshepisoSelepe if this answer solved your problem, then please mark it as the correct answer by clicking on the check mark at the left.
– Lajos Arpad
2 days ago
add a comment |
up vote
1
down vote
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
add a comment |
up vote
1
down vote
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
add a comment |
up vote
1
down vote
up vote
1
down vote
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
answered 2 days ago
Lajos Arpad
26.9k1861116
26.9k1861116
add a comment |
add a comment |
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%2f53409715%2fsql-server-date-convertion%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
1
Looks like your date format is set to MM/DD/YYYY
– jarlh
2 days ago
If you use default date format that won't happen:
2018-08-14– juergen d
2 days ago