Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - SQL for dates
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

SQL for dates

 Post Reply Post Reply
Author
suedechaser View Drop Down
Groupie
Groupie


Joined: 25 February 2006
Location: USA
Status: Offline
Points: 129
Post Options Post Options   Thanks (0) Thanks(0)   Quote suedechaser Quote  Post ReplyReply Direct Link To This Post Topic: SQL for dates
    Posted: 10 October 2007 at 11:55pm
Hi all,

I posted this question in another forum with no response - so i'll try here.

Could someone please show me corect MSSQL string would be for all records up to and including today.

Something like ...strSQL = strSQL + "WHERE th.my_date < OR = Now()"

I have searched and tried many options but cannot get anything to work.

Help Please??

Back to Top
dj air View Drop Down
Senior Member
Senior Member
Avatar

Joined: 05 April 2002
Location: United Kingdom
Status: Offline
Points: 3627
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj air Quote  Post ReplyReply Direct Link To This Post Posted: 11 October 2007 at 2:47pm
this depends on the database, the below is for MSSQL
 
strSQL = strSQL + "WHERE th.my_date < '" &  Now() &"' "
for access its this one below (not 100% sure if not try without the ' ', been ages since used access)
 
strSQL = strSQL + "WHERE th.my_date < '#" & Now() &"#' "
Back to Top
WebWiz-Bruce View Drop Down
Admin Group
Admin Group
Avatar
Web Wiz Developer

Joined: 03 September 2001
Location: Bournemouth
Status: Offline
Points: 9844
Post Options Post Options   Thanks (0) Thanks(0)   Quote WebWiz-Bruce Quote  Post ReplyReply Direct Link To This Post Posted: 11 October 2007 at 4:53pm
Not sure if Now() would work with SQL Server, but the following is taken from code I wrote yesterday to do the same thing:-

tblHostingService.Renewal_Date <= GetDate();

GetDate() is SQL Servers version of vbScripts Now()

This of course includes the time as well so isn't 100% accurate, there are other more precise methods but can't remember them off-hand.
Back to Top
suedechaser View Drop Down
Groupie
Groupie


Joined: 25 February 2006
Location: USA
Status: Offline
Points: 129
Post Options Post Options   Thanks (0) Thanks(0)   Quote suedechaser Quote  Post ReplyReply Direct Link To This Post Posted: 12 October 2007 at 1:18am
Hi dj / Borg,

Thanks. GetDate() appears to work well with < GetDate() and <=GetDate().

Why doesn't it work with = GetDate() or > GetDate()?

suede
Back to Top
shakir View Drop Down
Groupie
Groupie


Joined: 08 November 2007
Location: Saudi Arabia
Status: Offline
Points: 41
Post Options Post Options   Thanks (0) Thanks(0)   Quote shakir Quote  Post ReplyReply Direct Link To This Post Posted: 18 November 2007 at 7:48am
In SQL no OR try <=
after that check the date format, you need to use cast or convert to change the date

convert(datetime,DateField ,103)
Back to Top
WebWiz-Bruce View Drop Down
Admin Group
Admin Group
Avatar
Web Wiz Developer

Joined: 03 September 2001
Location: Bournemouth
Status: Offline
Points: 9844
Post Options Post Options   Thanks (0) Thanks(0)   Quote WebWiz-Bruce Quote  Post ReplyReply Direct Link To This Post Posted: 18 November 2007 at 9:39am
With SQL you can use BETWEEN eg:-

WHERE DBfield BETWEEN Date1 AND Date2

Replace DBfield with the name of the database field and Date1 and Date2 with the 2 dates
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.