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
 Between in sqlserver

Author  Topic 

Amjath
Yak Posting Veteran

66 Posts

Posted - 2007-07-26 : 06:01:05
Hi All,
i had 1 doubt regarding the between in sql server

when i between in sqlserver it means it includes that date also
for example
select * from tbltest where joindate between '1/2/2002' AND '2/2/2002'
in this case joindate having 2-2-2202 09:30:01.000 is not coming
why is it like this
what is need to add to get 2-2-2202 09:30:01.000 in between

With Regards
Amjath

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2007-07-26 : 06:05:27
As per the statement it will fetch all the records between date 1/2/2002 00:00:00.000 and 2/2/2002 00:00:00.000
select * from tbltest where joindate between '1/2/2002' AND '2/2/2002'

if you want to include time part as well. then you need to set it as

select * from tbltest where joindate between '1/2/2002' AND '2/2/2002 23:59:59.999'
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-07-26 : 08:10:38
This is the usual way to do a date range query:
>= start_datetime and < end_datetime

Also, date format YYYYMMDD hh:mm:ss.mil is the only unambiguous format for a date string.
Example: '20071231 23:59:59.997' or just '20071231' for the date.


select *
from
tbltest
where
joindate >= '20020201'' and
joindate < '20020203'


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -