sql convert string to date/datetime
up vote
0
down vote
favorite
My Filename column looks like 'D181115.T000000'. I used the following code to make it a string, looks like '2018-11-15'.
select '20' + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)
from table_name
Then I want to convert the string to date type (because I need to sort by date)
select convert(datetime, '20 + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)')
from table_name
Then I got this error message:
The data types varchar and varchar are incompatible in the subtract
operator.
Any help will be greatly appreciated!
sql string date sqldatatypes
|
show 1 more comment
up vote
0
down vote
favorite
My Filename column looks like 'D181115.T000000'. I used the following code to make it a string, looks like '2018-11-15'.
select '20' + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)
from table_name
Then I want to convert the string to date type (because I need to sort by date)
select convert(datetime, '20 + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)')
from table_name
Then I got this error message:
The data types varchar and varchar are incompatible in the subtract
operator.
Any help will be greatly appreciated!
sql string date sqldatatypes
2
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
You are missing a single quote after'20
, then it is doingSubstract
operation:20 + substring(filename, 2,2) +
minus20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
1
Are you using SQL Server? In that case you could use justselect cast('20' + substring('D181115.T000000', 2,6) as date)
orselect try_cast('20' + substring('D181115.T000000', 2,6) as date)
.YYYYMMDD
is a recognized, unambiguous date literal.YYYY-MM-DD
on the other hand depends on theDATEFORMAT
setting
– Panagiotis Kanavos
Nov 8 at 15:17
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.
– Igor
Nov 8 at 15:18
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date181115
was meant to be interpreted as'2018-11-15'
or'2018-15-11'
. Just something for thought.
– arahman
Nov 8 at 15:19
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My Filename column looks like 'D181115.T000000'. I used the following code to make it a string, looks like '2018-11-15'.
select '20' + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)
from table_name
Then I want to convert the string to date type (because I need to sort by date)
select convert(datetime, '20 + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)')
from table_name
Then I got this error message:
The data types varchar and varchar are incompatible in the subtract
operator.
Any help will be greatly appreciated!
sql string date sqldatatypes
My Filename column looks like 'D181115.T000000'. I used the following code to make it a string, looks like '2018-11-15'.
select '20' + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)
from table_name
Then I want to convert the string to date type (because I need to sort by date)
select convert(datetime, '20 + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)')
from table_name
Then I got this error message:
The data types varchar and varchar are incompatible in the subtract
operator.
Any help will be greatly appreciated!
sql string date sqldatatypes
sql string date sqldatatypes
edited Nov 8 at 15:10
jarlh
27.8k52137
27.8k52137
asked Nov 8 at 15:10
Feifei Zhang
408
408
2
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
You are missing a single quote after'20
, then it is doingSubstract
operation:20 + substring(filename, 2,2) +
minus20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
1
Are you using SQL Server? In that case you could use justselect cast('20' + substring('D181115.T000000', 2,6) as date)
orselect try_cast('20' + substring('D181115.T000000', 2,6) as date)
.YYYYMMDD
is a recognized, unambiguous date literal.YYYY-MM-DD
on the other hand depends on theDATEFORMAT
setting
– Panagiotis Kanavos
Nov 8 at 15:17
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.
– Igor
Nov 8 at 15:18
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date181115
was meant to be interpreted as'2018-11-15'
or'2018-15-11'
. Just something for thought.
– arahman
Nov 8 at 15:19
|
show 1 more comment
2
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
You are missing a single quote after'20
, then it is doingSubstract
operation:20 + substring(filename, 2,2) +
minus20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
1
Are you using SQL Server? In that case you could use justselect cast('20' + substring('D181115.T000000', 2,6) as date)
orselect try_cast('20' + substring('D181115.T000000', 2,6) as date)
.YYYYMMDD
is a recognized, unambiguous date literal.YYYY-MM-DD
on the other hand depends on theDATEFORMAT
setting
– Panagiotis Kanavos
Nov 8 at 15:17
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.
– Igor
Nov 8 at 15:18
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date181115
was meant to be interpreted as'2018-11-15'
or'2018-15-11'
. Just something for thought.
– arahman
Nov 8 at 15:19
2
2
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
You are missing a single quote after
'20
, then it is doing Substract
operation:20 + substring(filename, 2,2) +
minus 20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
You are missing a single quote after
'20
, then it is doing Substract
operation:20 + substring(filename, 2,2) +
minus 20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
1
1
Are you using SQL Server? In that case you could use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or select try_cast('20' + substring('D181115.T000000', 2,6) as date)
. YYYYMMDD
is a recognized, unambiguous date literal. YYYY-MM-DD
on the other hand depends on the DATEFORMAT
setting– Panagiotis Kanavos
Nov 8 at 15:17
Are you using SQL Server? In that case you could use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or select try_cast('20' + substring('D181115.T000000', 2,6) as date)
. YYYYMMDD
is a recognized, unambiguous date literal. YYYY-MM-DD
on the other hand depends on the DATEFORMAT
setting– Panagiotis Kanavos
Nov 8 at 15:17
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.– Igor
Nov 8 at 15:18
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.– Igor
Nov 8 at 15:18
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date
181115
was meant to be interpreted as '2018-11-15'
or '2018-15-11'
. Just something for thought.– arahman
Nov 8 at 15:19
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date
181115
was meant to be interpreted as '2018-11-15'
or '2018-15-11'
. Just something for thought.– arahman
Nov 8 at 15:19
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD
is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD
on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
add a comment |
up vote
1
down vote
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare @filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(@filename, 2,2) + '-' + substring(@filename, 4,2) + '-' + substring(@filename,6,2))
Produces output:
2018-11-15 00:00:00.000
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD
is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD
on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
add a comment |
up vote
4
down vote
accepted
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD
is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD
on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD
is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD
on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD
is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD
on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
edited Nov 8 at 15:22
answered Nov 8 at 15:19
Panagiotis Kanavos
52k478107
52k478107
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
add a comment |
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
Thanks for the suggestion. I will use 20181115 as the filename instead.
– Feifei Zhang
Nov 8 at 18:59
add a comment |
up vote
1
down vote
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare @filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(@filename, 2,2) + '-' + substring(@filename, 4,2) + '-' + substring(@filename,6,2))
Produces output:
2018-11-15 00:00:00.000
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
add a comment |
up vote
1
down vote
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare @filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(@filename, 2,2) + '-' + substring(@filename, 4,2) + '-' + substring(@filename,6,2))
Produces output:
2018-11-15 00:00:00.000
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
add a comment |
up vote
1
down vote
up vote
1
down vote
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare @filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(@filename, 2,2) + '-' + substring(@filename, 4,2) + '-' + substring(@filename,6,2))
Produces output:
2018-11-15 00:00:00.000
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare @filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(@filename, 2,2) + '-' + substring(@filename, 4,2) + '-' + substring(@filename,6,2))
Produces output:
2018-11-15 00:00:00.000
answered Nov 8 at 15:19
Igor
37.8k34397
37.8k34397
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
add a comment |
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
this is because i am following the example SELECT CONVERT(datetime, '2017-08-25'); I feel stupid. Thanks for the help!
– Feifei Zhang
Nov 8 at 16:03
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%2f53210575%2fsql-convert-string-to-date-datetime%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
2
Which dbms are you using? (Those queries are product specific.)
– jarlh
Nov 8 at 15:11
You are missing a single quote after
'20
, then it is doingSubstract
operation:20 + substring(filename, 2,2) +
minus20 + substring(filename, 2,2) + '
– LONG
Nov 8 at 15:14
1
Are you using SQL Server? In that case you could use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
orselect try_cast('20' + substring('D181115.T000000', 2,6) as date)
.YYYYMMDD
is a recognized, unambiguous date literal.YYYY-MM-DD
on the other hand depends on theDATEFORMAT
setting– Panagiotis Kanavos
Nov 8 at 15:17
Then I want to convert the string to date type
<= Look at the first and 2nd statements you included, they are different in that you are missing a lot of quotes in the second one.– Igor
Nov 8 at 15:18
You should be careful with string to date conversions. It is always better to start with date/datetime over a string date. You may never know in what format the string date
181115
was meant to be interpreted as'2018-11-15'
or'2018-15-11'
. Just something for thought.– arahman
Nov 8 at 15:19