Skip to content
Advertisement

Converting Date in Where clause in SQL

I need to return the sum of something within the last 30 days. My date field is a text field. My table looks something like this:

my Code looks like this:

This is the error I get “The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.”

I tried removing the convert function and got another error: “The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.”

So now I am stuck. Thanks in advance for the help

Advertisement

Answer

Because you have no other choice then to use a varchar as a date, then I would convert the one GETDATE() value to a varchar instead of converting all the rows to a date. It’s possible because they are stored as YYYYMMDD. I think that would be bettter for performance (for sure if you have an index on this date column).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement