-1

My database is SQL Server, and the thing is that "dates" are stored as a string type. If I execute that query it does not order properly.

What I want to do is, get each day's value for between 7am and 8 am. For example, I want to get 14th of June records for between 7am an

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    If you want to order by date, you'll have to convert it to date. One of many reasons why you should never, ever store dates as strings. – HoneyBadger Jul 29 '19 at 09:25
  • Possible duplicate of [SQL Server convert string to datetime](https://stackoverflow.com/questions/1135746/sql-server-convert-string-to-datetime) – Amira Bedhiafi Jul 29 '19 at 09:26
  • If it looks like a `DATE`, smells like a `DATE`, quacks like a `DATE` - ***why on earth*** aren't you storing it as `DATE` ?? ***ALWAYS*** use the **most appropriate** datatype - don't just store everything as a string .... – marc_s Jul 29 '19 at 09:45

1 Answers1

1

Use CAST() or CONVERT() in your WHERE / ORDER BY clause conditions. However, you should store dates as date types instead of strings to avoid such problems.

Pratik Bhavsar
  • 808
  • 8
  • 32