Solved

Time format in sql where clause

Posted on 2011-02-16
6
490 Views
Last Modified: 2012-05-11
I have the form where I have the time picker. The time that get passed to the sql s in this format 5:30 AM. How do i create a where clause in sql to search for this value
0
Comment
Question by:erikTsomik
  • 2
  • 2
  • 2
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
Pass the time in a parameter (best)
Otherwise, if it is varchar, then, depending on what you are comparing it to

WHERE time_column = CONVERT(datetime, '5:30 AM')

WHERE time_column > CONVERT(datetime, '5:30 AM')

dateadd(d, -datediff(d, 0, getdate()), time_column) < CONVERT(datetime, '5:30 AM')

etc

The last one compares that the TIME portion of a datetime field is less than 5:30 am (regardless of date)
0
 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
Also0 my data type for that fileld is datetime so the data that get stored (time portion) is in 24 hour clock . I think will need to convert left side of the where clause to match right side of the query
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
>>  How do i create a where clause in sql to search for this value

Do you want all the records logged after 5.30 AM? The last query by cyberkiwi should work to compare the time part only. You can also try like this.

CONVERT(datetime,CONVERT(varchar(20),time_field,14)) > select CONVERT(datetime,'05:30 AM')

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
@Erik

The column is supposed to be datetime. The right side is turned into valid [date]time to compare against it.
0
 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
How can i do in between operation. I am doing the following but it returns nothing. and I do have the records in the dataabse

bodyData BETWEEN CONVERT(datetime, '7:29 AM') and CONVERT(datetime, '8:41 AM')
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
try this.
CONVERT(datetime,CONVERT(varchar(20),bodyData ,14)) BETWEEN CONVERT(datetime, '7:29 AM') and CONVERT(datetime, '8:41 AM')

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Set the max value for a column 7 32
Passing value to a stored procedure 8 65
query help 18 50
SQL Query Syntax Error 9 29
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now