Author |
Topic |
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-10-15 : 05:00:06
|
hi,how can i get the date of yesterdaY??i tried this on my queries but it will be problem at the end of month i think.select * from xxx where datepart(day,date) = datepart(day,getdate()) -1 and datepart(month,date) = datepart(month,getdate()) and datepart(year,date) = datepart(year,getdate())well, if the yesterday is 31th of the month it wil lbe problemwhen today is christmas it will be problem from the datepart(year,date) function.what to do?MS BLESS US |
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-10-15 : 05:37:39
|
In case your datetime has a time part, you do it like this:SELECT * FROM xxxWHERE date >= DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0) AND date < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) and in case you don't, like this:SELECT * FROM xxxWHERE date = DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0) both also has the benefit that an index on 'date' can be used instead of a complete table scan (way faster).-- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don't know," Alice answered. "Then," said the cat, "it doesn't matter." |
 |
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-10-15 : 06:02:50
|
select dateadd(day,-1,getdate())i think this solves too =)MS BLESS US |
 |
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-10-15 : 14:00:53
|
Yes and no, it does give you the date of yesterday, but will contain the time part too, which in most cases is undesired, in your case too.-- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don't know," Alice answered. "Then," said the cat, "it doesn't matter." |
 |
|
Kristen
Test
22859 Posts |
|
mk.vignesh
Starting Member
3 Posts |
Posted - 2006-10-17 : 01:45:28
|
U CAN TRY OUTSELECT GETDATE()-1IT GIVES EXACTLY THIS TIME YESTERDAYNobody is Perfect in this world,I am Nobody |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-17 : 10:35:17
|
www.sql-server-performance.com/fk_datetime.aspMadhivananFailing to plan is Planning to fail |
 |
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2006-10-17 : 12:02:22
|
Does no one use the day function anymore? select day(getdate()-1)Mike"oh, that monkey is going to pay" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-18 : 09:48:42
|
quote: Originally posted by mfemenel Does no one use the day function anymore? select day(getdate()-1)Mike"oh, that monkey is going to pay"
It wont make use of index if that column has itRead the article I postedMadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-18 : 10:08:38
|
quote: Originally posted by mfemenel Does no one use the day function anymore? select day(getdate()-1)
It will be hard to do any searches with only day portion of a date.Peter LarssonHelsingborg, Sweden |
 |
|
|