ASP SQL - using variables in SQL select screen

Ask a Question related to ASP, Design and Development.

  1. #1

    Default ASP SQL - using variables in SQL select screen

    I have a form that sends an ASP page the data to use for this string...
    using values for "startdate", "enddate" and "lookfor" varibables...

    rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' & startdate & ' AND
    dateadded < ' & enddate & ' AND sendemail = ' & lookfor & '"

    well it works perfectly when I have the actual dates or the actual value of
    "lookfor" like this example

    rs.Open "SELECT * FROM TABLE WHERE dateadded >= ('6/15/2003 2:12:03 PM')
    AND dateadded < ('8/15/2003 2:12:03 PM') AND sendemail = 'Y'"


    but when I use the top string with variables instead of values i get error:
    Microsoft OLE DB Provider for SQL Server error '80040e07'

    Syntax error converting datetime from character string.

    /newsstats/reports.asp, line 23


    If I use response.Writes before the error happens i get what seem to be
    correct values for the variables?!?!

    ('7/29/2003 12:00:00 AM')
    ('8/4/2003 12:00:00 AM')
    'Y'

    what am I doing wrong?? :(

    --
    - Ed


    Ed Garcia Guest

  2. Similar Questions and Discussions

    1. video full screen and screen savers
      Why does full-screen mode not disable the system screen saver in the flash player but does so for AIR? In the documentation it says "For AIR...
    2. passing variables from 'select' element in a form
      Hi, I have 2 'select' element in a form. One is populated with a list of names and the other is empty Users can select 1 or more names from the...
    3. passing variables from select elements in form
      I have an interesting problem that I'm trying to solve. I have 2 'select' element in a form. One is populated with a list of names and the other...
    4. Asp variables in sql select
      Hello, Is such select sql statment allowed? " SELECT * FROM Photo_SubCategory Where (Status = 1 And Session("id") <> "") or (Status = 0 And...
    5. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
      Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id,
  3. #2

    Default Re: ASP SQL - using variables in SQL select screen

    You need to response.write the entire sql statement to be able to debug
    this. Always assign your SQL statement to a variable so you can
    response.write it. Then use the variable in your recordset open statement.

    Show us the result of the response.write if you still can't figure it out.

    You may want to use a stored procedure instead of building the sql statement
    dynamically. It's easy to pass parameters to a procedure. In this case, you
    could run a script like this to create the procedure:

    CREATE PROCEDURE GetRecords (
    @start datetime,
    @end datetime,
    @lookfor char(1) --just a guess
    ) As
    Select col1, ...,colN FROM Table
    WHERE dateadded >= @start AND
    dateadded < @end AND
    sendemail = @lookfor

    Then in ASP, do this:
    conn.GetRecords cdate(startdate), cdate(enddate), _
    lookfor, rs

    Bob Barrows

    Ed Garcia wrote:
    > I have a form that sends an ASP page the data to use for this
    > string... using values for "startdate", "enddate" and "lookfor"
    > varibables...
    >
    > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' & startdate & ' AND
    > dateadded < ' & enddate & ' AND sendemail = ' & lookfor & '"
    >
    > well it works perfectly when I have the actual dates or the actual
    > value of "lookfor" like this example
    >
    > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ('6/15/2003 2:12:03
    > PM') AND dateadded < ('8/15/2003 2:12:03 PM') AND sendemail = 'Y'"
    >
    >
    > but when I use the top string with variables instead of values i get
    > error: Microsoft OLE DB Provider for SQL Server error '80040e07'
    >
    > Syntax error converting datetime from character string.
    >
    > /newsstats/reports.asp, line 23
    >
    >
    > If I use response.Writes before the error happens i get what seem to
    > be correct values for the variables?!?!
    >
    > ('7/29/2003 12:00:00 AM')
    > ('8/4/2003 12:00:00 AM')
    > 'Y'
    >
    > what am I doing wrong?? :(

    Bob Barrows Guest

  4. #3

    Default Re: ASP SQL - using variables in SQL select screen

    I swear I just heard an airplane fly above my head... did I mentioned I am
    relatively new at this? :)

    Ok let me thinker that you gave me a bit and try it...

    --
    - Ed

    "Bob Barrows" <reb_01501@yahoo.com> wrote in message
    news:ekDFSoQXDHA.3444@tk2msftngp13.phx.gbl...
    > You need to response.write the entire sql statement to be able to debug
    > this. Always assign your SQL statement to a variable so you can
    > response.write it. Then use the variable in your recordset open statement.
    >
    > Show us the result of the response.write if you still can't figure it out.
    >
    > You may want to use a stored procedure instead of building the sql
    statement
    > dynamically. It's easy to pass parameters to a procedure. In this case,
    you
    > could run a script like this to create the procedure:
    >
    > CREATE PROCEDURE GetRecords (
    > @start datetime,
    > @end datetime,
    > @lookfor char(1) --just a guess
    > ) As
    > Select col1, ...,colN FROM Table
    > WHERE dateadded >= @start AND
    > dateadded < @end AND
    > sendemail = @lookfor
    >
    > Then in ASP, do this:
    > conn.GetRecords cdate(startdate), cdate(enddate), _
    > lookfor, rs
    >
    > Bob Barrows
    >
    > Ed Garcia wrote:
    > > I have a form that sends an ASP page the data to use for this
    > > string... using values for "startdate", "enddate" and "lookfor"
    > > varibables...
    > >
    > > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' & startdate & ' AND
    > > dateadded < ' & enddate & ' AND sendemail = ' & lookfor & '"
    > >
    > > well it works perfectly when I have the actual dates or the actual
    > > value of "lookfor" like this example
    > >
    > > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ('6/15/2003 2:12:03
    > > PM') AND dateadded < ('8/15/2003 2:12:03 PM') AND sendemail = 'Y'"
    > >
    > >
    > > but when I use the top string with variables instead of values i get
    > > error: Microsoft OLE DB Provider for SQL Server error '80040e07'
    > >
    > > Syntax error converting datetime from character string.
    > >
    > > /newsstats/reports.asp, line 23
    > >
    > >
    > > If I use response.Writes before the error happens i get what seem to
    > > be correct values for the variables?!?!
    > >
    > > ('7/29/2003 12:00:00 AM')
    > > ('8/4/2003 12:00:00 AM')
    > > 'Y'
    > >
    > > what am I doing wrong?? :(
    >
    >

    Ed Garcia Guest

  5. #4

    Default Re: ASP SQL - using variables in SQL select screen

    thanks for the tip of making this a complete sql statement before tyring to
    actually get it... it worked after I did this...

    selectstring = "SELECT * FROM TABLE WHERE dateadded >= " + startdate + "
    AND lastvisit < " + enddate + " AND sendemail = '" + lookfor + "'"

    rs.Open (selectstring), sConnString, 3

    --
    - Ed


    "Ed Garcia" <ed@askme.com> wrote in message
    news:OvA0V4QXDHA.2516@TK2MSFTNGP09.phx.gbl...
    > I swear I just heard an airplane fly above my head... did I mentioned I am
    > relatively new at this? :)
    >
    > Ok let me thinker that you gave me a bit and try it...
    >
    > --
    > - Ed
    >
    > "Bob Barrows" <reb_01501@yahoo.com> wrote in message
    > news:ekDFSoQXDHA.3444@tk2msftngp13.phx.gbl...
    > > You need to response.write the entire sql statement to be able to debug
    > > this. Always assign your SQL statement to a variable so you can
    > > response.write it. Then use the variable in your recordset open
    statement.
    > >
    > > Show us the result of the response.write if you still can't figure it
    out.
    > >
    > > You may want to use a stored procedure instead of building the sql
    > statement
    > > dynamically. It's easy to pass parameters to a procedure. In this case,
    > you
    > > could run a script like this to create the procedure:
    > >
    > > CREATE PROCEDURE GetRecords (
    > > @start datetime,
    > > @end datetime,
    > > @lookfor char(1) --just a guess
    > > ) As
    > > Select col1, ...,colN FROM Table
    > > WHERE dateadded >= @start AND
    > > dateadded < @end AND
    > > sendemail = @lookfor
    > >
    > > Then in ASP, do this:
    > > conn.GetRecords cdate(startdate), cdate(enddate), _
    > > lookfor, rs
    > >
    > > Bob Barrows
    > >
    > > Ed Garcia wrote:
    > > > I have a form that sends an ASP page the data to use for this
    > > > string... using values for "startdate", "enddate" and "lookfor"
    > > > varibables...
    > > >
    > > > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' & startdate & ' AND
    > > > dateadded < ' & enddate & ' AND sendemail = ' & lookfor & '"
    > > >
    > > > well it works perfectly when I have the actual dates or the actual
    > > > value of "lookfor" like this example
    > > >
    > > > rs.Open "SELECT * FROM TABLE WHERE dateadded >= ('6/15/2003 2:12:03
    > > > PM') AND dateadded < ('8/15/2003 2:12:03 PM') AND sendemail = 'Y'"
    > > >
    > > >
    > > > but when I use the top string with variables instead of values i get
    > > > error: Microsoft OLE DB Provider for SQL Server error '80040e07'
    > > >
    > > > Syntax error converting datetime from character string.
    > > >
    > > > /newsstats/reports.asp, line 23
    > > >
    > > >
    > > > If I use response.Writes before the error happens i get what seem to
    > > > be correct values for the variables?!?!
    > > >
    > > > ('7/29/2003 12:00:00 AM')
    > > > ('8/4/2003 12:00:00 AM')
    > > > 'Y'
    > > >
    > > > what am I doing wrong?? :(
    > >
    > >
    >
    >

    Ed Garcia Guest

  6. #5

    Default Re: ASP SQL - using variables in SQL select screen

    In article <eFasogQXDHA.416@tk2msftngp13.phx.gbl>, [email]ed@askme.com[/email] says...
    x> I have a form that sends an ASP page the data to use for this
    string...
    > using values for "startdate", "enddate" and "lookfor" varibables...
    > If I use response.Writes before the error happens i get what seem to be
    > correct values for the variables?!?!
    >
    > ('7/29/2003 12:00:00 AM')
    > ('8/4/2003 12:00:00 AM')
    > 'Y'
    >
    > what am I doing wrong?? :(
    If you're accessing the Jet engine, you need to surround your dates with
    #-signs, or so I've heard.

    -- Rick

    Guinness Mann Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139