?
Solved

Time format in sql where clause

Posted on 2011-02-16
6
Medium Priority
?
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34911857
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
ID: 34912111
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 41

Expert Comment

by:Sharath
ID: 34912287
>>  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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34912887
@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
ID: 34917391
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 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 34917515
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

771 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