Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query on just time of a datetime column

Author  Topic 

tschacherl
Starting Member

2 Posts

Posted - 2012-01-23 : 17:33:23
I have a table using the datetime data type and I'm trying to query based on just the time. For example, I have a table of shows and there are two shows that are named the same but they are at different times. One is at 7pm and the other at 8:30pm.

Everything else is the same between the shows and both shows happen every day, which means I actually have lots of records for the two shows. One for each show each day for six months. Thus when I want to query on say the early show, I have no way to differentiate it from the late show because I'm unable to query just on the time part of the datetime field that contains the date and time of the show.

This is running on MS SQL server 2005. Any suggestions? Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-23 : 17:51:11
Use the convert function with style 108: convert(varchar(10), getdate(), 108). See BOL for details.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tschacherl
Starting Member

2 Posts

Posted - 2012-01-24 : 16:04:53
Thanks tkizer, that's what I was looking for. I thought I had tried it before but this is what I got to work...

select *,convert(varchar(10), eventDate, 108) as eventTime from [database].[dbo].[table] WHERE eventAct = 'show name' and convert(varchar(10), eventDate, 108) = '19:30:00'

Of course I didn't need the "as eventTime" part but I just wanted to see what I was comparing too as I was getting messed up before by using the .000 part of the time. It's not part of the 108 format. I'm sure you know all this but I thought this might be useful for others with a similar question.

Thanks for your help.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-24 : 16:06:40
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-24 : 16:11:29
Do you even care about the seconds?


SELECT CONVERT(varchar(5), GetDate(), 108)



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -