|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
VBScript - Database - Recordset - Filtering by part of stored date?
Afternoon,
I've tried googling but can't find the appropriate syntax for what i need, felt it best to ask!! I have a drop down menu that passes month and year variables through a post, i.e. if the user selects January 2010 it will return as the querystring Quote:
In the database table, stored in the 'date' field, is a short date dd/MM/yyyy. What syntax should i use to finalise this select statement when i want to select all records for the chosen month and year? Code:
SELECT * from dailystock WHERE date querystring month and year bit here ORDER BY date DESC Any advice appreciated! |
|
#2
|
|||
|
|||
|
Quote:
Something like Code:
StartDateStr = "1 " & MonthName(Month(month)) & " " & year
EndDate = DateAdd("m",1,StartDate)
EndDateStr = Day(EndDate) & " " & MonthName(Month(EndDate)) & " " & Year(EndDate)
SELECT * from dailystock WHERE date between '" & StartDateStr & "' and '" & EndDateStr & "' ORDER BY date DESC
|
|
#3
|
||||
|
||||
|
Jonathan's suggestions looks good, I just wanted to add that there are built in functions which allow you to return specifc parts of a date. The syntax is dependant upon the type of database you are using but in SQLServer you can use DatePart:
Code:
SELECT * from dailystock WHERE DATEPART(Month, [date]) = " & Request.Querystring("month") & " " & _
"AND DATEPART(year, [date]) = " & Request.Querystring("year") & " " & _
"ORDER BY [date] DESC"
I think that Access uses Month/Year, eg: Code:
SELECT * from dailystock WHERE Month([date]) = " & Request.Querystring("month") & " " & _
"AND Year([date]) = " & Request.Querystring("year") & " " & _
"ORDER BY [date] DESC"
And in Oracle you can use TO_CHAR to specify a date format that ignores days, something like: Code:
SELECT * from dailystock WHERE TO_CHAR(date, 'MON-YYYY') = '" & MonthName(Request.Querystring("month"), True) & "-" & _
Request.Querystring("year") & "' " & _
"ORDER BY date DESC"
|
|
#4
|
|||
|
|||
|
Looks good to me sync, I'll give it a whack in the morning when I get to the office.
Thanks! |
|
#5
|
|||
|
|||
|
Using VBScript within asp pages, so a small modification to accept it was needed, and i now get a syntax error:
Code:
objRS = objConn.Execute("SELECT * from dailystock WHERE DATEPART(Month," & Request.Querystring("month") & ") AND DATEPART(year," & Request.Querystring("year") & ") ORDER BY date DESC")
|
|
#6
|
||||
|
||||
|
You need to specify the name of your field that contains the date, the format for datepart is DATEPART(datepart, fieldname), try this:
Code:
objRS = objConn.Execute("SELECT * from dailystock WHERE DATEPART(Month,[date]) = " & Request.Querystring("month") & " AND DATEPART(year, [date]) = " & Request.Querystring("year") & " ORDER BY date DESC")
|
|
#7
|
|||
|
|||
|
Thanks for the help sync, but unfortunately still a syntax error.... specifically:
Quote:
Version info: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 |
|
#8
|
||||
|
||||
|
You havent said what type of database you are using, is it sqlserver?
Also, can you post the contents of your sql statement so we can see exactly what is being executed? Code:
Dim sql
sql = "SELECT * from dailystock WHERE DATEPART(Month,[date]) = " & Request.Querystring("month") & " AND DATEPART(year, [date]) = " & Request.Querystring("year") & " ORDER BY date DESC"
Response.Write(sql)
Response.End
objRS = objConn.Execute(sql)
|
|
#9
|
||||
|
||||
|
Having re-read your error message it looks as if you are using MYSQL, if this is the case I think you need to use Day/Month/Year as my Access example. Check out the MYSQL date commands
The more info you give us from the start the better the reply you will get. |
|
#10
|
|||
|
|||
|
Still getting the same syntax error.... i've tried it with and without the '[]' around the date field, still no joy...
Code:
objRS = objConn.Execute("SELECT * from dailystock WHERE Month(date) = " & Request.Querystring("month") & " AND Year(date) = " & Request.Querystring("year") & " ORDER BY date DESC")
This is starting to give me a headache! |
|
#11
|
||||
|
||||
|
Quote:
Anyway, post the output of the query here and if you can, then run it in MySql's query runner. |
|
#12
|
|||
|
|||
|
Quote:
Write out your query string before attempting to execute it to make sure there is noting wrong with it Code:
Response.Write "SELECT * from dailystock WHERE Month(date) = " & Request.Querystring("month") & " AND Year(date) = " & Request.Querystring("year") & " ORDER BY date DESC"
|
|
#13
|
|||
|
|||
|
Thanks Jon - turns out the form isn't submitting the inputted month/year data... works fine if i manually type in the querystring to the url.
If anyone has a clue why this isn't passing the month and year through the url, please enlighten me!! works fine on every other page!! Code:
<form action="stocktotals.aspx?filter=yes" method="post" name="filter"> <div align="center">Filter historical data by date range<br /> <select name="month" id="month"> <option> </option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="year" id="year"> <option> </option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> </select> <input type="submit" value="Go!" /> </div></form> |
|
#14
|
|||
|
|||
|
Quote:
Because you're using method="post" and you should be using method="get". For Post use Request.Form For Get use Request.QueryString |
|
#15
|
||||
|
||||
|
You are using POST in your form method, so you should use
Request.Form("month") |
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > VBScript - Database - Recordset - Filtering by part of stored date? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|