SQL Query - Select Time

Hi Experts,
This is my query:
SELECT * FROM WorkSchedules WHERE StartTime <= '1899-12-30 09:30:00.000'
How can I ignore the date and select records that are earlier than 09:30
I would like to achieve this because when a record is updated, it becomes 1900-01-01 09:30:00.000 in the SQL Server database, and my query won't return the expected rows.
I would appreciate your help.
romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
WHERE (DATEPART(HOUR, StartTime) < 9 OR
              (DATEPART(HOUR, StartTime) = 9 AND DATEPART(MINUTE, StartTime) <= 30))

Note: That is not "sargable" and could force a scan of the entire table.

Btw, why are you using a base date of "1899-12-30"?  In SQL, it's vastly more common to use 1900-01-01.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romsomIT DeveloperAuthor Commented:
Thank you very much, this is perfect!
0
Brian CroweDatabase AdministratorCommented:
Note: This is going to require a table scan since the predicate is non-sargable

SELECT *
FROM WorkSchedules
WHERE CAST(StartTime AS TIME) < '09:30'
1
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

romsomIT DeveloperAuthor Commented:
Scott, I'm importing the data from an Excel sheet to SQL Server.
After the import the StartTime column automatically adds the 1899-12-30 date, although in my Excel sheet the time is just '09:30'
When a user updates a record from the web site, the date changes to 1900-01-01.
Thank you for your help, your solution was perfect!
0
romsomIT DeveloperAuthor Commented:
Brian, I've tried your solution as well, and it also returns the expected results. Sorry, I already closed the question.
0
PortletPaulEE Topic AdvisorCommented:
The "base date" in Excel is different to that of SQL Server, which is why you are getting 1899-12-30

If you only have the 2 dates to deal with, then this may be faster.

SELECT * FROM WorkSchedules
 WHERE StartTime between  '1899-12-30 00:00:00.000' and '1899-12-30 09:30:00.000'
 OR StartTime between  '1900-01-01 00:00:00.000' and '1900-01-01 09:30:00.000'

But if you aren't interested in the date, why not have a computed&persisted column that is just of the time?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.