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!










share|improve this question



















  • 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 doing Substract operation:20 + substring(filename, 2,2) + minus 20 + 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) 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










  • 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














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!










share|improve this question



















  • 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 doing Substract operation:20 + substring(filename, 2,2) + minus 20 + 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) 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










  • 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












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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 doing Substract operation:20 + substring(filename, 2,2) + minus 20 + 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) 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










  • 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












  • 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 doing Substract operation:20 + substring(filename, 2,2) + minus 20 + 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) 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










  • 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







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












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






share|improve this answer






















  • Thanks for the suggestion. I will use 20181115 as the filename instead.
    – Feifei Zhang
    Nov 8 at 18:59

















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





share|improve this answer




















  • 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











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',
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
);



);













 

draft saved


draft discarded


















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

























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






share|improve this answer






















  • Thanks for the suggestion. I will use 20181115 as the filename instead.
    – Feifei Zhang
    Nov 8 at 18:59














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






share|improve this answer






















  • Thanks for the suggestion. I will use 20181115 as the filename instead.
    – Feifei Zhang
    Nov 8 at 18:59












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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












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





share|improve this answer




















  • 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















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





share|improve this answer




















  • 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













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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)