August 21, 2010 at 3:06 am
Hi,
I wanna retrieve records for today. But not working?
Any idea?
select freedate from holidays where freedate = (getdate())
how can I retrieve for specific date?
rgds,
zhtway
August 21, 2010 at 3:24 am
zawhtway (8/21/2010)
Hi,I wanna retrieve records for today. But not working?
Any idea?
select freedate from holidays where freedate = (getdate())
how can I retrieve for specific date?
rgds,
zhtway
You can do it like this:
SET DATEFORMAT DMY --This date format is for dd/mm/yyyy
SELECT FREEDATE FROM HOLIDAYS WHERE FREEDATE = '01/01/2010' --this is for the specific date
or you can declare the variable that contain current date
DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = GETDATE()
SELECT FREEDATE FROM HOLIDAYS WHERE FREEDATE = @CURRENTDATE
😎
August 21, 2010 at 4:11 am
Or,
DATEADD(dd, o, DATEDIFF(dd, 0,GETDATE()))
August 22, 2010 at 2:45 am
One more solution to your problem
SELECT * FROM holidays WHERE DATEDIFF( DAY, freedate, GETDATE() ) = 0
The below mentioned article might be helpful to you
http://www.sqlservercentral.com/articles/T-SQL/65806/
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 22, 2010 at 3:56 am
Or curiously
SELECT freedate FROM holidays WHERE freedate = {t '00:00:00'}
August 22, 2010 at 8:41 am
One thing that all of the above are assuming is that the freedate column contains only dates, with the times set to 00:00:00.000. If this is not the case, then you need to use a range check to do it:
SELECT FREEDATE
FROM HOLIDAYS
WHERE FREEDATE >= DateAdd(day, DateDiff(day, 0, GetDate()), 0)
AND FREEDATE < DateAdd(day, DateDiff(day, 0, GetDate())+1, 0)
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 22, 2010 at 8:46 am
Kingston Dhasian (8/22/2010)
One more solution to your problem
SELECT * FROM holidays WHERE DATEDIFF( DAY, freedate, GETDATE() ) = 0
The below mentioned article might be helpful to you
That kills al usefull uses of indexes.
August 22, 2010 at 7:48 pm
Kingston Dhasian (8/22/2010)
One more solution to your problem
SELECT * FROM holidays WHERE DATEDIFF( DAY, freedate, GETDATE() ) = 0
The below mentioned article might be helpful to you
Ummm... no. Sorry, Kingston. If you do that, it makes the predicate non sargeable and there's no chance of an index on FreeDate working as an INDEX SEEK.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 22, 2010 at 7:49 pm
Ah... apologies. I see that Remi already posted such a comment. I've really got to learn to read a whole thread before posting.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 23, 2010 at 12:20 am
Jeff Moden (8/22/2010)
Kingston Dhasian (8/22/2010)
One more solution to your problem
SELECT * FROM holidays WHERE DATEDIFF( DAY, freedate, GETDATE() ) = 0
The below mentioned article might be helpful to you
Ummm... no. Sorry, Kingston. If you do that, it makes the predicate non sargeable and there's no chance of an index on FreeDate working as an INDEX SEEK.
I agree with both of you( Jeff and Remi ). This will surely not use an INDEX SEEK and will kill performance. I will definitely not use this method in stored procedures for applications. I would prefer Wayne's method instead in that case.
But i mostly use this method to write a quick query. It is short and sweet.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 23, 2010 at 4:44 am
Jeff Moden (8/22/2010)
Ah... apologies. I see that Remi already posted such a comment. I've really got to learn to read a whole thread before posting.
Same here... I had to re-learn that one pretty much every few weeks :w00t:.
August 23, 2010 at 6:23 am
Kingston Dhasian (8/23/2010)
But i mostly use this method to write a quick query. It is short and sweet.
... and slow. Heh... I normally want the highest speed when I need to "write a quick query" because I have to sit there an wait for the results. Also, why practice hitting the wrong notes on the piano just because you don't want to play the full song? 😛 If you always do it right, it'll get easier to always do it right. :w00t:
(Heh... sorry for the mini-rant, Kingston, but you've hit a couple of sore spots with me on this one. 🙂 )
--Jeff Moden
Change is inevitable... Change for the better is not.
August 23, 2010 at 7:07 am
Jeff Moden (8/23/2010)
Kingston Dhasian (8/23/2010)
But i mostly use this method to write a quick query. It is short and sweet.... and slow. Heh... I normally want the highest speed when I need to "write a quick query" because I have to sit there an wait for the results. Also, why practice hitting the wrong notes on the piano just because you don't want to play the full song? 😛 If you always do it right, it'll get easier to always do it right. :w00t:
(Heh... sorry for the mini-rant, Kingston, but you've hit a couple of sore spots with me on this one. 🙂 )
The term "write a quick query" actually referred to the typing speed and not the execution speed. I never meant to say that the method will be fast in the performance sense.
But you have a point where you said "If you always do it right, it'll get easier to always do it right". It might make me add the non performing code inadvertently into some stored procedure some day. So, point taken:-)
And i am never going to feel bad if you point out my mistakes.:-). So, no problems.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 23, 2010 at 3:24 pm
Hi All,
Thanks you guys..
Now I learn alot from all of you.
rgds,
zhtway
August 23, 2010 at 9:56 pm
Thanks, Kingston. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply