SELECT Id,Date,Name
FROM people
WHERE DATEPART(hh,Date) >= 7
AND DATEPART(hh,Date) <= 8
Order by DATEPART(ww,Date);
My database is SQL Server, and the thing is that “Date” 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 and 8am, samething for 15th of june and so on….
First that all you (we) should know how it is stored in the DB. To parse the “varchar date” to a datetime.
Let suppose we have the Date varchar storaged like mm/dd/yyyy
so You should use a
SELECT convert(datetime, THEDATECOLUMN, 101) -- mm/dd/yyyyYou can take a look to more types of convert varchar to datetime here
Ok, in the understanding that you have to convert first the varchar to date time and in this example using the
mm/dd/yyyywe should write the query like this way.Depending of how it is storaged, you should change the type of conversion listed in the related link