Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting all the data using time greater than 4pm

I need to select the Data containing time > 4pm in datatimestamp every day in SQL Server Management Studio Microsoft SQL Server 2005 - 9.00.4060.00 (X64) DB table which has two years of data. What's the best way to do this? My time stamp has following format:DATETIME, '2005-10-13 16:00:00', 102. I have data at random times every afternoon. I need to get the data after 4pm for every day. Not just for one day.

For example i tried for one day like this:

SELECT     Yield, Date, ProductType, Direct
FROM         MIAC_CCYX
WHERE     (Date < CONVERT(DATETIME, '2005-10-13 16:00:00', 102))  Thanks for help
like image 781
user1783998 Avatar asked Nov 26 '12 20:11

user1783998


People also ask

What is the best data type for time?

The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

How do I use Smalldatetime in SQL?

smalldatetime descriptionhh is two digits, ranging from 00 to 23, that represent the hour. mm is two digits, ranging from 00 to 59, that represent the minute. ss is two digits, ranging from 00 to 59, that represent the second. Values that are 29.998 seconds or less are rounded down to the nearest minute.

How do you select the maximum of a column?

Discussion: To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.


1 Answers

It's hard to read your question, but assuming you really are using a datetime data type, you can use datepart to find any dates with a time greater than 4 PM:

WHERE datepart(hh, YourDate) > 16

Since you now need minutes as well, if you want records after 4:45 PM, you can cast your date to a time like this:

SQL Server 2000/2005

SELECT Yield, [Date], ProductType, Direct 
FROM MIAC_CCYX 
WHERE cast(convert(char(8), [Date], 108) as datetime) > cast('16:45' as datetime)

Essentially you cast the date using convert's Date and Time styles to convert the date to a time string, then convert back to a datetime for comparison against your desired time.

SQL Server 2008+

SELECT Yield, [Date], ProductType, Direct 
FROM MIAC_CCYX 
WHERE CAST([Date] as time) > CAST('16:45' as time)
like image 167
LittleBobbyTables - Au Revoir Avatar answered Oct 23 '22 01:10

LittleBobbyTables - Au Revoir