|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Help with SQL Statement
Trying to get this SQL statement to work, but still fairly new to writing SQL statements. The code above and below the commented lines work, just cant get the line of code in between to work. Can someone please help? Thanks.
SQL_ITEMS = "SELECT * "_ &"From Autos " _ &"WHERE Category LIKE '"_ & Request("selCategory1")_ &"%' "_ &"AND Type LIKE '%"_ & Request("selType") _ &"%' "_ &"AND ItemName LIKE '%"_ & ItemName _ &"%' "_ &"AND Description LIKE '%"_ & ItemDesc _ &"%' "_ '''''''''this is the part im having problems with &"AND Year >= Request("selYearFrom") &"AND Year <= Request("selYearTo") &"AND Price >= Request("selPriceFrom") &"AND Price <= Request("selPriceTo") '''''''''''''''''''''''''''''''''''''''''''''''''' &"AND DatePlaced + 45 >= Now()"_ &"Order By DatePlaced DESC;" |
|
#2
|
|||
|
|||
|
Try this
&"AND Year >='" & Request("selYearFrom") & "'" &"AND Year <= '" & Request("selYearTo") & "'" &"AND Price >= " & Request("selPriceFrom") &"AND Price <= " & Request("selPriceTo") Also, it is dangerous to let user data get into your sql statment without being filtered by your code. You should inspect the data coming from request("whatever") to prevent sql injection attacks & data errors. |
|
#3
|
|||
|
|||
|
Doug, I tried the code and this is the error I get
&"AND Year <= '" & Request("selYearTo") & "'" ^ The ^ sign appears under the first & sign. |
|
#4
|
|||
|
|||
|
his code should work, just make sure you add the concatenate operator at the end to continue the string to the next line.
also you have comments after and before the part that's not working. I don't think you can continue a line with a comment inbetween, you'd have to remove the comment or on the link after the comments continue the string with string = string & "...." i think. |
|
#5
|
|||
|
|||
|
I take it that you'd like to show records that are less then 45 days old right? This is using MsSql as your database. The date portion of the select statement would be slightly different for Access. Hope it's close to what you're looking for.
<% set db = server.createobject("adodb.connection") db.connectionString = "provider=sqloledb.1; user id=MyUserId; password=MyPassword; data source=MyServer; initial catalog=MyDatabase" db.mode=3 db.cursorlocation=3 db.open SelCategory1 = request("selCategory1") SelType = request("selType") ItemName = request("ItemName") ItemDesc = request("ItemDesc") SelYearFrom = request("SelYearFrom") SelYearTo = request("SelYearTo") SelPriceFrom = request("SelPriceFrom") SelPriceTo = request("SelPriceTo") MyDate = Date()-45 sql = "select * from Autos" sql = sql & " where Category LIKE '" & SelCategory1 & "%' " sql = sql & " and Type LIKE '%" & Request("selType") & "%' " sql = sql & " and ItemName LIKE '%" & ItemName & "%' " sql = sql & " and Description LIKE '%" & ItemDesc & "%' " sql = sql & " and Year >= '" & SelYearFrom & "'" sql = sql & " and Year <= '" & SelYearTo & "'" sql = sql & " and Price >= '" & SelPriceFrom & "'" sql = sql & " and Price <= '" & SelPriceTo & "'" sql = sql & " and DatePlaced <= '" & MyDate & "'" sql = sql & " order by DatePlaced desc" rs.open sql, db if not rs.eof then do while not rs.eof response.write rs("Field1") & "<br>" rs.movenext loop end if rs.close set rs = nothing %> |
|
#6
|
|||
|
|||
|
Oops! Change...
sql = sql & " and Type LIKE '%" & Request("selType") & "%' " To sql = sql & " and Type LIKE '%" & SelType & "%' " |
|
#7
|
|||
|
|||
|
Oops! Try this again...
<% set db = server.createobject("adodb.connection") db.connectionString = "provider=sqloledb.1; user id=MyUserId; password=MyPassword; data source=MyServer; initial catalog=MyDatabase" db.mode=3 db.cursorlocation=3 db.open SelCategory1 = request("selCategory1") SelType = request("selType") ItemName = request("ItemName") ItemDesc = request("ItemDesc") SelYearFrom = request("SelYearFrom") SelYearTo = request("SelYearTo") SelPriceFrom = request("SelPriceFrom") SelPriceTo = request("SelPriceTo") MyDate = Date()-45 sql = "select * from Autos" sql = sql & " where Category LIKE '" & SelCategory1 & "%'" sql = sql & " and Type LIKE '%" & SelType & "%'" sql = sql & " and ItemName LIKE '%" & ItemName & "%'" sql = sql & " and Description LIKE '%" & ItemDesc & "%'" sql = sql & " and Year >= '" & SelYearFrom & "'" sql = sql & " and Year <= '" & SelYearTo & "'" sql = sql & " and Price >= '" & SelPriceFrom & "'" sql = sql & " and Price <= '" & SelPriceTo & "'" sql = sql & " and DatePlaced <= '" & MyDate & "'" sql = sql & " order by DatePlaced desc" rs.open sql, db if not rs.eof then do while not rs.eof response.write rs("Field1") & "<br>" rs.movenext loop end if rs.close set rs = nothing %> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > Help with SQL Statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|