SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old March 28th, 2008, 06:19 AM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
Query - General - How do I search between 2 dates?

Hi guys,

Hopefully someone knows th answer to this, i've done a bit of searching but haven't quite found what i'm looking for. I want to be able to run a query that searches a table (tblservice) and displays all the records in the last week.

But i'm not sure how to compare between todays date and last weeks date. i have a field in my table with the date that gets entered im the format d/mm/yy/

Any ideas? Thanks!

Dean

Reply With Quote
  #2  
Old March 28th, 2008, 08:24 AM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
You don't say what db engine you have -- many of the Date/Time functions are language dependent. Also, I hope your date column is a DateTime format or your in for a world of hurt.

In Oracle, it would be
SELECT * FROM table WHERE aDate BETWEEN TRUNC(SYSDATE - 7) AND TRUNC(SYSDATE)
__________________
Wolffy
------------------------
Teaching people to fish.

Reply With Quote
  #3  
Old March 28th, 2008, 01:31 PM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
I'm using an access database and yes i have it in a date/time format

Reply With Quote
  #4  
Old March 28th, 2008, 02:04 PM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
Quote:
Originally Posted by dfaz
I'm using an access database and yes i have it in a date/time format



SELECT * FROM table WHERE aDate BETWEEN DateAdd("ww", -1 Date()) AND Date()

Reply With Quote
  #5  
Old April 14th, 2008, 10:09 AM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
Thanks for getting back to me Wolfy, i put the code in a dn it cam up with an error that said "End of statement expected"

Also in the SQL string is aDate supposed be a variable set by me? I have simply copied the coe you gave and changed my tablename.

Another thing worth noting is that i have noticed that access changes my data around, i put this code in to create the date:

Day(Now) & "/" & month(Now) & "/" & year(Now)
i.e. 14/4/2008

and the access field is set up for short date, when i recall a record the date would be 4/14/2008.

any ideas with the sql string?

thanks

Dfaz

Reply With Quote
  #6  
Old April 14th, 2008, 10:22 AM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
End of statement expected -- either a missing paren or quote. Since you didn't post yoru code, I can't tell.
aDate is a column in your table.
Why are you doing all that work to store a date:
INSERT INTO table (aDate) VALUE(Date())
Dates are stored as a number of ticks since zero-hour. Access will display the date based on the locale of you computer (I believe...I'm not an Access guru).

Reply With Quote
  #7  
Old April 19th, 2008, 02:00 PM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
Quote:
Originally Posted by Wolffy
Why are you doing all that work to store a date:
INSERT INTO table (aDate) VALUE(Date())


Not sure really, the record is created and saved on a different page and i wanted the date to be displayed to the user on the page when they opened the page. When saving the record i then just used the value of the text box the date was saved in. I'll change my code.

Anyway the code i'm using is as follows:

Code:
'Create  connection string
    Dim connString As New String(ConfigurationSettings.AppSettings("ConnString"))
				 		
		
    'Open a connection
     objConnection = New OleDbConnection(ConnString)
     objConnection.Open()
    
    'Specify the SQL string
	StrSQL = "SELECT * FROM tblrequests WHERE RDate BETWEEN DateAdd("ww", -1 Date()) AND Date()"
	
	adapter = new OleDbDataAdapter( StrSQL, connstring )

		ds = new DataSet				
		adapter.Fill (ds)
		
dg.DataSource = ds ' assign the dataset to the datagrid

dg.DataBind() 'bind the data to the datagrid

End Sub

Rdate being the name of my field in the table

thanks for all the continued help

Dfaz

Reply With Quote
  #8  
Old April 21st, 2008, 01:45 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 9,962 micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 3 Months 2 Weeks 4 Days 13 h 54 m 16 sec
Reputation Power: 1275
i think u r missing a comma in ur sql, try this
Code:
StrSQL = "SELECT * FROM tblrequests WHERE RDate BETWEEN DateAdd("ww", -1, Date()) AND Date()"
Comments on this post
Wolffy agrees: Yup, looks like a dropped a comma back in my example.
__________________
Laziness is my religion and Sunday is my God

Get the Mantra!

Reply With Quote
  #9  
Old April 22nd, 2008, 03:00 PM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
Ok, i have changed the SQl as advised and the error "BC30205: End of statement expected." still came up. Code as follows, any other ideas

Code:

'Create  connection string
    Dim connString As New String(ConfigurationSettings.AppSettings("ConnString"))
				 		
		
    'Open a connection
     objConnection = New OleDbConnection(ConnString)
     objConnection.Open()
    
    'Specify the SQL string
	StrSQL = "SELECT * FROM tblrequests WHERE RDate BETWEEN DateAdd("ww", -1, Date()) AND Date()"
	
	adapter = new OleDbDataAdapter( StrSQL, connstring )

		ds = new DataSet				
		adapter.Fill (ds)
		
dg.DataSource = ds ' assign the dataset to the datagrid

dg.DataBind() 'bind the data to the datagrid

End Sub

Reply With Quote
  #10  
Old April 22nd, 2008, 03:13 PM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
It's the double quotes around the ww -- change 'em to single quotes. Yup, I had double quotes in my example.
Comments on this post
dfaz agrees!

Reply With Quote
  #11  
Old April 22nd, 2008, 03:18 PM
dfaz dfaz is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 56 dfaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 11 m 55 sec
Reputation Power: 1
I did wonder about that, but was not sure so didn't mention it. Anyway my code is working fine now,

Wolffy and Micky thank you very much for your help.

Dfaz

Reply With Quote
  #12  
Old April 22nd, 2008, 03:20 PM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
Glad you got it sorted -- sorry I double-duffed on my example.

Reply With Quote
  #13  
Old April 24th, 2008, 02:48 AM
micky's Avatar
micky micky is offline
Couch Potato Wizard
Click here for more information. Click here for more information
 
Join Date: Jan 2005
Location: India
Posts: 9,962 micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)micky User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 1480 Folding Title: Novice Folder
Time spent in forums: 3 Months 2 Weeks 4 Days 13 h 54 m 16 sec
Reputation Power: 1275
glad i cud help

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Query - General - How do I search between 2 dates?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump