Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Selecting Records with Yesterday's Date?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Selecting Records with Yesterday's Date?

 Post Reply Post Reply
Author
Brolin99 View Drop Down
Groupie
Groupie
Avatar

Joined: 03 May 2003
Location: New Zealand
Status: Offline
Points: 58
Post Options Post Options   Thanks (0) Thanks(0)   Quote Brolin99 Quote  Post ReplyReply Direct Link To This Post Topic: Selecting Records with Yesterday's Date?
    Posted: 19 May 2008 at 5:29am
Hi there,
 
I have written a script where I want to select a series of records that feature yesterday's date - regardless of the time.
 
I have a standard date/time field with the date and time information inside it, and I want to run a query along the lines of:
 
SELECT * FROM tblItems WHERE DATE = #Yesterday's Date#
 
I am running SQL Server 2005.
 
Any help would be greatly appreciated.
 
Thanks
Back to Top
Jono View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 18 September 2006
Location: United Kingdom
Status: Offline
Points: 100
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jono Quote  Post ReplyReply Direct Link To This Post Posted: 19 May 2008 at 6:12pm
I would think you'd have to use the GETDATE() function. However this returns the time as well: print getdate()
To do this you'd need to take on the date portions. i.e. less than today and greater than two days ago.
Back to Top
Jono View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 18 September 2006
Location: United Kingdom
Status: Offline
Points: 100
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jono Quote  Post ReplyReply Direct Link To This Post Posted: 19 May 2008 at 6:18pm
So the full answer is:
 
SELECT * FROM tblItems WHERE date>=(CAST( FLOOR( CAST( GETDATE() AS FLOAT ) ) AS DATETIME ) -1) AND date<=(CAST( FLOOR( CAST( GETDATE() AS FLOAT ) ) AS DATETIME ))
It doesn't look neat, but it is fast and does what you want. The -1 after the first DATETIME keyword removed a day from today (giving yesterday), the other one is from midnight today (so you may want < rather than <=).
Back to Top
Scotty32 View Drop Down
Moderator Group
Moderator Group


Joined: 30 November 2002
Location: Manchester, UK
Status: Offline
Points: 1682
Post Options Post Options   Thanks (0) Thanks(0)   Quote Scotty32 Quote  Post ReplyReply Direct Link To This Post Posted: 19 May 2008 at 6:18pm
Would this not work?

strDate = DateDiff('d',-1, Date())
SELECT * FROM tblItems WHERE DATE = '" & strDate & "'



You can see more on the DateDiff Function on my site, which will explain each part.
S2H.co.uk - WebWiz Mods and Skins

For support on my mods + skins, please use my forum.
Back to Top
Jono View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 18 September 2006
Location: United Kingdom
Status: Offline
Points: 100
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jono Quote  Post ReplyReply Direct Link To This Post Posted: 19 May 2008 at 8:01pm

Won't that only match the specific date, not the date range? I suspect you'd have to trim to date field in the database to get it to work.

Back to Top
Scotty32 View Drop Down
Moderator Group
Moderator Group


Joined: 30 November 2002
Location: Manchester, UK
Status: Offline
Points: 1682
Post Options Post Options   Thanks (0) Thanks(0)   Quote Scotty32 Quote  Post ReplyReply Direct Link To This Post Posted: 20 May 2008 at 5:52pm
I thought all they wanted was items from yesterday.

Which means it should work.
S2H.co.uk - WebWiz Mods and Skins

For support on my mods + skins, please use my forum.
Back to Top
Jono View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 18 September 2006
Location: United Kingdom
Status: Offline
Points: 100
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jono Quote  Post ReplyReply Direct Link To This Post Posted: 20 May 2008 at 10:03pm
I believe it'll only match the exact date that you've entered (in this case the day + 0 hours, mins and seconds). You'd have to extract only the date part from underliying database to perform this type of comparison.
I'm only using SQL (which also stores the time as well as the date), it may work in Access, but i think that stores the time also?
Back to Top
Brolin99 View Drop Down
Groupie
Groupie
Avatar

Joined: 03 May 2003
Location: New Zealand
Status: Offline
Points: 58
Post Options Post Options   Thanks (0) Thanks(0)   Quote Brolin99 Quote  Post ReplyReply Direct Link To This Post Posted: 25 May 2008 at 1:13am
Thanks guys - worked a treat - much appreciated Smile
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.