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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select between Dates

Author  Topic 

dimepop
Starting Member

33 Posts

Posted - 2012-08-28 : 08:24:49
Hi if i have a column with date like:
28/08/2012 13:37:14

If i want my query to select only between:
24/06/2012 08:20
and
25/06/2012 15:20

How can i do it?

Thanks



Select * From Table
Where Date like ??

Andy Hyslop
Starting Member

14 Posts

Posted - 2012-08-28 : 08:54:11
[code]
WHERE
<YOUR_COLUMN>
BETWEEN '24/06/2012 08:20' and '25/06/2012 15:20'
[/code]

Or even better

[code]
WHERE
<YOUR_COLUMN>
>= '24/06/2012 08:20' OR <YOUR_COLUMN> <= '25/06/2012 15:20'
[/code]

Andy
Go to Top of Page

dimepop
Starting Member

33 Posts

Posted - 2012-08-28 : 08:57:43
I'll try that, thanks
Go to Top of Page

dimepop
Starting Member

33 Posts

Posted - 2012-08-28 : 09:25:38
Hi Andy, thanks for your help.
How can i select the next one?
from 11/08/2012 to 21/08/2012 only between 14:00 and 14:30
Would this be Ok?
I don't have enough data so i am not sure if it will work

TimeAndDate BETWEEN '28/08/2012' and '24/08/2012'
and TimeAndDate between '12:20' and '13:20'

Would this work?




quote:
Originally posted by Andy Hyslop


WHERE
<YOUR_COLUMN>
BETWEEN '24/06/2012 08:20' and '25/06/2012 15:20'


Or even better


WHERE
<YOUR_COLUMN>
>= '24/06/2012 08:20' OR <YOUR_COLUMN> <= '25/06/2012 15:20'


Andy

Go to Top of Page

dimepop
Starting Member

33 Posts

Posted - 2012-08-28 : 12:07:14
Hi, just tried my query, it doesn't fail, but it doesn't show any data.
Can you help?
Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-08-28 : 14:30:08
when you use the BETWEEN operator the first precidate needs to be less than the second (order matters):
DECLARE @Foo TABLE (TimeAndDate DATETIME)

INSERT @Foo
VALUES
('2012-08-25T11:00:00.000'),
('2012-08-25T12:30:00.000'),
('2012-08-25T14:00:00.000'),
('2012-08-26T12:30:00.000'),
('2012-08-20T12:30:00.000')

SELECT *
FROM @Foo
WHERE
TimeAndDate BETWEEN '2012-08-24' AND '2012-08-28'
and CAST(TimeAndDate AS TIME) between CAST('12:20:00' AS TIME) and CAST('13:20:00' AS TIME)
Go to Top of Page
   

- Advertisement -