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
 Entering date+time condition in WHERE statement?

Author  Topic 

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-08-16 : 04:37:00
Hi all

I am trying to enter a time condition as part of my SQL statement below. I can get the date to work but I am trying to enter a specific time for >= and <= (i.e. something like 13:05:59 and 23:59:59) but it doesn't seem to work.

Any tips on how to enter time condition along with the date? I tried googling for the format but they did not work so I asked

SELECT
ce.enquiry_number,
ce.logged_date,
ce.outstanding_flag,
enquiry_status.enq_status_name,
ao.officer_name,
enquiry.site_code,
enquiry.plot_number

FROM
ce,
enquiry_status,
ao,
enquiry

WHERE
ce.enq_status_code = enquiry_status.enq_status_code AND
ao.officer_code = ce.officer_code AND
ce.enquiry_number = enquiry.enquiry_number AND
ce.logged_date >= '20061201' AND
ce.logged_date <= '20061231'

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-08-16 : 04:45:46
Why cant you use date and time combinely like '20061201 23:59:59' ?

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-16 : 04:57:34
Always use open-ended datetime searches!

SELECT * FROM Table1
WHERE DateTimeColumn >= '20061201' AND DateTimeColumn < '20070101'

This is how you avoid ANY problem regarding the time part.


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-08-16 : 05:31:24
quote:
Originally posted by vaibhavktiwari83

Why cant you use date and time combinely like '20061201 23:59:59' ?

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER



This was exactly what I was looking for. Cheers!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-16 : 05:32:45
I hope you are joking.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-08-16 : 06:51:33
quote:
Originally posted by Peso

I hope you are joking.



N 56°04'39.26"
E 12°55'05.63"




Agreed with you...

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-16 : 10:50:29
quote:
Originally posted by Maverick_

quote:
Originally posted by vaibhavktiwari83

Why cant you use date and time combinely like '20061201 23:59:59' ?

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER



This was exactly what I was looking for. Cheers!


are you looking at data between two dates with exact time part?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-17 : 02:56:12
quote:
Originally posted by vaibhavktiwari83

Why cant you use date and time combinely like '20061201 23:59:59' ?

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER


Becuase it will not bring all data of date 20061201

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-08-17 : 03:06:37
quote:
Originally posted by madhivanan

quote:
Originally posted by vaibhavktiwari83

Why cant you use date and time combinely like '20061201 23:59:59' ?

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER


Becuase it will not bring all data of date 20061201

Madhivanan

Failing to plan is Planning to fail



Even he did not want all the data of the date 20061201

see his original post

"I can get the date to work but I am trying to enter a specific time for >= and <= (i.e. something like 13:05:59 and 23:59:59)"


Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page
   

- Advertisement -