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.

3
  • This is my table I want to convert to datetime (currently its char(24)) imgur.com/a/TyFob5N Commented Dec 31, 2018 at 22:14
  • 1
    What is the format of the date in this column and which version of Sql Server are you using? Commented Dec 31, 2018 at 22:15
  • I'm using SQL 2016. Format of the date in this column is 2018-12-31 12:11:17.679 Commented Dec 31, 2018 at 22:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 . . . Well, you cannot convert the values implicitly then.
But even if I select non-null values, and convert it to datetime datatype, it is still giving me the conversion error.
@123testing123 . . . As explained in the answer, the last query shows you the values that cannot be converted.
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())

13 Comments

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?
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.
I'm getting the data directly from SSRS but its being stored as a char.
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
My ultimate goal is to select the records from the past hour but I can't do that because I have to convert my counterdatetime first to datetime right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.