• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Time format in sql where clause

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
erikTsomik
Asked:
erikTsomik
  • 2
  • 2
  • 2
1 Solution
 
cyberkiwiCommented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
SharathData EngineerCommented:
>>  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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
cyberkiwiCommented:
@Erik

The column is supposed to be datetime. The right side is turned into valid [date]time to compare against it.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
SharathData EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now