SQL Server: Conversion failed when converting time from character string












0















I have a column below that has datatype char(24) but the data contains a date



I want to convert the data to datetime so that I can select the data from the past hour like this:



Where CounterDateTime   >= DateAdd(hour, -1, getDate())


But I keep getting an error:




Conversion failed when converting date and/or time from character string




even if I convert my CounterDateTime to datetime. Please help.










share|improve this question

























  • This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

    – 123testing123
    Dec 31 '18 at 22:14








  • 1





    What is the format of the date in this column and which version of Sql Server are you using?

    – forpas
    Dec 31 '18 at 22:15











  • I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

    – 123testing123
    Dec 31 '18 at 22:17
















0















I have a column below that has datatype char(24) but the data contains a date



I want to convert the data to datetime so that I can select the data from the past hour like this:



Where CounterDateTime   >= DateAdd(hour, -1, getDate())


But I keep getting an error:




Conversion failed when converting date and/or time from character string




even if I convert my CounterDateTime to datetime. Please help.










share|improve this question

























  • This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

    – 123testing123
    Dec 31 '18 at 22:14








  • 1





    What is the format of the date in this column and which version of Sql Server are you using?

    – forpas
    Dec 31 '18 at 22:15











  • I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

    – 123testing123
    Dec 31 '18 at 22:17














0












0








0








I have a column below that has datatype char(24) but the data contains a date



I want to convert the data to datetime so that I can select the data from the past hour like this:



Where CounterDateTime   >= DateAdd(hour, -1, getDate())


But I keep getting an error:




Conversion failed when converting date and/or time from character string




even if I convert my CounterDateTime to datetime. Please help.










share|improve this question
















I have a column below that has datatype char(24) but the data contains a date



I want to convert the data to datetime so that I can select the data from the past hour like this:



Where CounterDateTime   >= DateAdd(hour, -1, getDate())


But I keep getting an error:




Conversion failed when converting date and/or time from character string




even if I convert my CounterDateTime to datetime. Please help.







sql sql-server datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 9:19









marc_s

580k13011191266




580k13011191266










asked Dec 31 '18 at 22:11









123testing123123testing123

86




86













  • This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

    – 123testing123
    Dec 31 '18 at 22:14








  • 1





    What is the format of the date in this column and which version of Sql Server are you using?

    – forpas
    Dec 31 '18 at 22:15











  • I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

    – 123testing123
    Dec 31 '18 at 22:17



















  • This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

    – 123testing123
    Dec 31 '18 at 22:14








  • 1





    What is the format of the date in this column and which version of Sql Server are you using?

    – forpas
    Dec 31 '18 at 22:15











  • I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

    – 123testing123
    Dec 31 '18 at 22:17

















This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

– 123testing123
Dec 31 '18 at 22:14







This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N

– 123testing123
Dec 31 '18 at 22:14






1




1





What is the format of the date in this column and which version of Sql Server are you using?

– forpas
Dec 31 '18 at 22:15





What is the format of the date in this column and which version of Sql Server are you using?

– forpas
Dec 31 '18 at 22:15













I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

– 123testing123
Dec 31 '18 at 22:17





I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679

– 123testing123
Dec 31 '18 at 22:17












2 Answers
2






active

oldest

votes


















0














You can cast the column to datetime like this:



Where CounterDateTime IS NOT NULL AND Cast(RTRIM(LTRIM(CounterDateTime)) as DateTime) >= DateAdd(hour, -1, getDate())


this could also work:



Where CounterDateTime IS NOT NULL AND CONVERT(datetime, RTRIM(LTRIM(CounterDateTime)), 121) >= DateAdd(hour, -1, getDate())


and 1 more:



Where CounterDateTime IS NOT NULL AND try_parse(RTRIM(LTRIM(CounterDateTime)) as DateTime using 'en-US') >= DateAdd(hour, -1, getDate())





share|improve this answer


























  • I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

    – 123testing123
    Dec 31 '18 at 22:34











  • See my edited answer

    – forpas
    Dec 31 '18 at 22:35











  • I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

    – 123testing123
    Dec 31 '18 at 22:39













  • I'm getting the data directly from SSRS but its being stored as a char.

    – 123testing123
    Dec 31 '18 at 22:41











  • If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

    – forpas
    Dec 31 '18 at 22:42



















0














Don't store date/time values as strings. This can cause a problem.



The next big issue is relying on implicit conversion. If you have to convert the values, do so explicitly. So:



Where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())


You clearly have values that cannot be converted. You don't know why. You can find these values using a query:



select CounterDateTime
from t
where try_convert(datetime, CounterDateTime) is null and
CounterDateTime is not null;


This will return the non-NULL values that cannot be converted.






share|improve this answer


























  • Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

    – 123testing123
    Dec 31 '18 at 22:33











  • @123testing123 . . . Well, you cannot convert the values implicitly then.

    – Gordon Linoff
    Dec 31 '18 at 22:35











  • But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

    – 123testing123
    Dec 31 '18 at 22:45






  • 1





    @123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

    – Gordon Linoff
    Dec 31 '18 at 23:06











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991792%2fsql-server-conversion-failed-when-converting-time-from-character-string%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









0














You can cast the column to datetime like this:



Where CounterDateTime IS NOT NULL AND Cast(RTRIM(LTRIM(CounterDateTime)) as DateTime) >= DateAdd(hour, -1, getDate())


this could also work:



Where CounterDateTime IS NOT NULL AND CONVERT(datetime, RTRIM(LTRIM(CounterDateTime)), 121) >= DateAdd(hour, -1, getDate())


and 1 more:



Where CounterDateTime IS NOT NULL AND try_parse(RTRIM(LTRIM(CounterDateTime)) as DateTime using 'en-US') >= DateAdd(hour, -1, getDate())





share|improve this answer


























  • I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

    – 123testing123
    Dec 31 '18 at 22:34











  • See my edited answer

    – forpas
    Dec 31 '18 at 22:35











  • I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

    – 123testing123
    Dec 31 '18 at 22:39













  • I'm getting the data directly from SSRS but its being stored as a char.

    – 123testing123
    Dec 31 '18 at 22:41











  • If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

    – forpas
    Dec 31 '18 at 22:42
















0














You can cast the column to datetime like this:



Where CounterDateTime IS NOT NULL AND Cast(RTRIM(LTRIM(CounterDateTime)) as DateTime) >= DateAdd(hour, -1, getDate())


this could also work:



Where CounterDateTime IS NOT NULL AND CONVERT(datetime, RTRIM(LTRIM(CounterDateTime)), 121) >= DateAdd(hour, -1, getDate())


and 1 more:



Where CounterDateTime IS NOT NULL AND try_parse(RTRIM(LTRIM(CounterDateTime)) as DateTime using 'en-US') >= DateAdd(hour, -1, getDate())





share|improve this answer


























  • I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

    – 123testing123
    Dec 31 '18 at 22:34











  • See my edited answer

    – forpas
    Dec 31 '18 at 22:35











  • I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

    – 123testing123
    Dec 31 '18 at 22:39













  • I'm getting the data directly from SSRS but its being stored as a char.

    – 123testing123
    Dec 31 '18 at 22:41











  • If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

    – forpas
    Dec 31 '18 at 22:42














0












0








0







You can cast the column to datetime like this:



Where CounterDateTime IS NOT NULL AND Cast(RTRIM(LTRIM(CounterDateTime)) as DateTime) >= DateAdd(hour, -1, getDate())


this could also work:



Where CounterDateTime IS NOT NULL AND CONVERT(datetime, RTRIM(LTRIM(CounterDateTime)), 121) >= DateAdd(hour, -1, getDate())


and 1 more:



Where CounterDateTime IS NOT NULL AND try_parse(RTRIM(LTRIM(CounterDateTime)) as DateTime using 'en-US') >= DateAdd(hour, -1, getDate())





share|improve this answer















You can cast the column to datetime like this:



Where CounterDateTime IS NOT NULL AND Cast(RTRIM(LTRIM(CounterDateTime)) as DateTime) >= DateAdd(hour, -1, getDate())


this could also work:



Where CounterDateTime IS NOT NULL AND CONVERT(datetime, RTRIM(LTRIM(CounterDateTime)), 121) >= DateAdd(hour, -1, getDate())


and 1 more:



Where CounterDateTime IS NOT NULL AND try_parse(RTRIM(LTRIM(CounterDateTime)) as DateTime using 'en-US') >= DateAdd(hour, -1, getDate())






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 23:04

























answered Dec 31 '18 at 22:21









forpasforpas

15.7k3626




15.7k3626













  • I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

    – 123testing123
    Dec 31 '18 at 22:34











  • See my edited answer

    – forpas
    Dec 31 '18 at 22:35











  • I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

    – 123testing123
    Dec 31 '18 at 22:39













  • I'm getting the data directly from SSRS but its being stored as a char.

    – 123testing123
    Dec 31 '18 at 22:41











  • If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

    – forpas
    Dec 31 '18 at 22:42



















  • I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

    – 123testing123
    Dec 31 '18 at 22:34











  • See my edited answer

    – forpas
    Dec 31 '18 at 22:35











  • I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

    – 123testing123
    Dec 31 '18 at 22:39













  • I'm getting the data directly from SSRS but its being stored as a char.

    – 123testing123
    Dec 31 '18 at 22:41











  • If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

    – forpas
    Dec 31 '18 at 22:42

















I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

– 123testing123
Dec 31 '18 at 22:34





I do have null values for counterdate time so I think this is why its not working. How do I convert valid/non-null value only?

– 123testing123
Dec 31 '18 at 22:34













See my edited answer

– forpas
Dec 31 '18 at 22:35





See my edited answer

– forpas
Dec 31 '18 at 22:35













I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

– 123testing123
Dec 31 '18 at 22:39







I've tried : "Where CounterDateTime IS NOT NULL AND CONVERT(datetime, CounterDateTime, 121) >= DateAdd(hour, -1, getDate())" but it is still giving me Conversion failed when converting date and/or time from character string.

– 123testing123
Dec 31 '18 at 22:39















I'm getting the data directly from SSRS but its being stored as a char.

– 123testing123
Dec 31 '18 at 22:41





I'm getting the data directly from SSRS but its being stored as a char.

– 123testing123
Dec 31 '18 at 22:41













If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

– forpas
Dec 31 '18 at 22:42





If you have tried both solutions and none works then it's possible that 2018-12-31 12:11:17.679 is not the format you are getting

– forpas
Dec 31 '18 at 22:42













0














Don't store date/time values as strings. This can cause a problem.



The next big issue is relying on implicit conversion. If you have to convert the values, do so explicitly. So:



Where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())


You clearly have values that cannot be converted. You don't know why. You can find these values using a query:



select CounterDateTime
from t
where try_convert(datetime, CounterDateTime) is null and
CounterDateTime is not null;


This will return the non-NULL values that cannot be converted.






share|improve this answer


























  • Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

    – 123testing123
    Dec 31 '18 at 22:33











  • @123testing123 . . . Well, you cannot convert the values implicitly then.

    – Gordon Linoff
    Dec 31 '18 at 22:35











  • But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

    – 123testing123
    Dec 31 '18 at 22:45






  • 1





    @123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

    – Gordon Linoff
    Dec 31 '18 at 23:06
















0














Don't store date/time values as strings. This can cause a problem.



The next big issue is relying on implicit conversion. If you have to convert the values, do so explicitly. So:



Where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())


You clearly have values that cannot be converted. You don't know why. You can find these values using a query:



select CounterDateTime
from t
where try_convert(datetime, CounterDateTime) is null and
CounterDateTime is not null;


This will return the non-NULL values that cannot be converted.






share|improve this answer


























  • Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

    – 123testing123
    Dec 31 '18 at 22:33











  • @123testing123 . . . Well, you cannot convert the values implicitly then.

    – Gordon Linoff
    Dec 31 '18 at 22:35











  • But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

    – 123testing123
    Dec 31 '18 at 22:45






  • 1





    @123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

    – Gordon Linoff
    Dec 31 '18 at 23:06














0












0








0







Don't store date/time values as strings. This can cause a problem.



The next big issue is relying on implicit conversion. If you have to convert the values, do so explicitly. So:



Where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())


You clearly have values that cannot be converted. You don't know why. You can find these values using a query:



select CounterDateTime
from t
where try_convert(datetime, CounterDateTime) is null and
CounterDateTime is not null;


This will return the non-NULL values that cannot be converted.






share|improve this answer















Don't store date/time values as strings. This can cause a problem.



The next big issue is relying on implicit conversion. If you have to convert the values, do so explicitly. So:



Where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())


You clearly have values that cannot be converted. You don't know why. You can find these values using a query:



select CounterDateTime
from t
where try_convert(datetime, CounterDateTime) is null and
CounterDateTime is not null;


This will return the non-NULL values that cannot be converted.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 22:34

























answered Dec 31 '18 at 22:23









Gordon LinoffGordon Linoff

783k35310414




783k35310414













  • Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

    – 123testing123
    Dec 31 '18 at 22:33











  • @123testing123 . . . Well, you cannot convert the values implicitly then.

    – Gordon Linoff
    Dec 31 '18 at 22:35











  • But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

    – 123testing123
    Dec 31 '18 at 22:45






  • 1





    @123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

    – Gordon Linoff
    Dec 31 '18 at 23:06



















  • Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

    – 123testing123
    Dec 31 '18 at 22:33











  • @123testing123 . . . Well, you cannot convert the values implicitly then.

    – Gordon Linoff
    Dec 31 '18 at 22:35











  • But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

    – 123testing123
    Dec 31 '18 at 22:45






  • 1





    @123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

    – Gordon Linoff
    Dec 31 '18 at 23:06

















Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

– 123testing123
Dec 31 '18 at 22:33





Hello Gordon, yes I do have null values. when I tried your first query "where try_convert(datetime, CounterDateTime) >= DateAdd(hour, -1, getDate())" I get an empty table

– 123testing123
Dec 31 '18 at 22:33













@123testing123 . . . Well, you cannot convert the values implicitly then.

– Gordon Linoff
Dec 31 '18 at 22:35





@123testing123 . . . Well, you cannot convert the values implicitly then.

– Gordon Linoff
Dec 31 '18 at 22:35













But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

– 123testing123
Dec 31 '18 at 22:45





But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.

– 123testing123
Dec 31 '18 at 22:45




1




1





@123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

– Gordon Linoff
Dec 31 '18 at 23:06





@123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.

– Gordon Linoff
Dec 31 '18 at 23:06


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991792%2fsql-server-conversion-failed-when-converting-time-from-character-string%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter