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
 Other SQL Server 2008 Topics
 dateTime Query

Author  Topic 

Edward M
Starting Member

2 Posts

Posted - 2009-03-08 : 18:01:45
Hi

I'm fairly new to writing SQL queries and could do with some advice. I have a table called booking in which there is a row called date.

I want to write a query that when a user enters 14/02/09 for example it pulls up every entry on that day.

I tried

SELECT *
FROM Booking
WHERE Date LIKE @Date + '%'

but that gives me a error stating it cant match the dateTime to a varChar2. I have searched the net for examples but can't seem to find what I need, can anyone help at all?

Thanks

hey001us
Posting Yak Master

185 Posts

Posted - 2009-03-08 : 20:28:26
SET @Date = '14/02/09'
I assume the date field is Date Time Data type.
Try this!
SELECT *
FROM Booking
WHERE Date between @Date + '00:00:00' and @Date + '23:59:59'


hey
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-09 : 05:47:46
More effecient

SELECT *
FROM Booking
WHERE Date >=@Date and Date<dateadd(day,1,@date)



Madhivanan

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

Edward M
Starting Member

2 Posts

Posted - 2009-03-10 : 08:28:27
Great, Thanks for the help on this, really appreciate help.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-10 : 08:32:41
quote:
Originally posted by Edward M

Great, Thanks for the help on this, really appreciate help.


You are welcome
Let me know which one you have decided to use

Madhivanan

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

GustiX
Starting Member

28 Posts

Posted - 2009-03-13 : 11:57:09
Hi

You might also try to cast the @Date variable to date.
SQL Server 2008 has a date datatype

SELECT *
FROM Booking
WHERE Date = cast(@Date as date)
Go to Top of Page
   

- Advertisement -