AlfredGG
AlfredGG

Reputation: 11

SQL Query in VBScript based on the date entered

I have a VBscript that queries the database to pull data out that is based on the shift e.g. graveyard, day, and swing. I need to adjust the time by an hour less only on days that are past 1-6-2019.

The solution I have tried is expanding my if statement and adding an AND function but it won't work because the first if statement is still true.

dim intCoilCount, intTotalSeconds,intSeconds,strDate,strShift

'SQL="select timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from TABLEEEE by timeStamp"
strShift=Request.Form("SHIFT")
strDate=Request.Form("StartDate")

'if date is greater than 2-22-2006 (switchover date) use SCALEFACTOR
'-----start-----------------
if datediff("d",strDate,cdate("2/22/2006")) <= 0 then
  SCALEFACTOR=30000.0 / 50.0
else
  SCALEFACTOR=1
end if

'-----end-----------------

'Fixed scale factor problem
'-----start-----------------
SCALEFACTOR=1
'-----end-----------------

SQL="select timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from entryCoilData" 
if strShift="graveyard" then
    SQL = SQL & " where timestamp > '" & cdate(strDate)-1 & " " & "11:00PM" & "'" & _
                " and timestamp <= '" & strDate & " " & "7:00AM" & "'"

elseif strShift="graveyard" and strDate >= cdate(1-6-2019) then
    SQL = SQL & " where timestamp > '" & strDate & " " & "10:00AM" & "'" & _
                " and timestamp <= '" & strDate & " " & "2:00PM" & "'"

elseif strShift="day" then
    SQL = SQL & " where timestamp > '" & strDate & " " & "7:00AM" & "'" & _
                " and timestamp <= '" & strDate & " " & "3:00PM" & "'"

elseif strShift="day" and strDate >= cdate(1-6-2019) then
    SQL = SQL & " where timestamp > '" & strDate & " " & "7:00AM" & "'" & _
                " and timestamp <= '" & strDate & " " & "3:00PM" & "'"

else
    SQL = SQL & " where timestamp > '" & strDate & " " & "3:00PM" & "'" & _
                " and timestamp <= '" & strDate & " " & "11:00PM" & "'"
end if

Upvotes: 0

Views: 1695

Answers (1)

Erik Oosterwaal
Erik Oosterwaal

Reputation: 4374

I would keep that pesky logic out of the SQL string, and do that in vbscript. Something like this (untested):

dim givendate, startdatetime, enddatetime
givendate = cdate(strDate)
startdatetime = CDate(strDate & " " & "3:00PM")
enddatetime = CDate(strDate & " " & "11:00PM")

if strShift="graveyard" then
    if givendate >= cdate("1-6-2019") then
        startdatetime = CDate(strDate & " " & "10:00AM")
        enddatetime = CDate(strDate & " " & "02:00PM")
    else
        startdatetime = DateADD("d", -1, CDate(strDate & " " & "11:00PM"))
        enddatetime = CDate(strDate & " " & "07:00AM")
    end if
end if

if strShift="day" then
    startdatetime = CDate(strDate & " " & "07:00PM")
    enddatetime = CDate(strDate & " " & "03:00PM")
end if

SQL="SELECT timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from entryCoilData" 
SQL = SQL & " WHERE timestamp > '" & startdatetime  & "'"
SQL = SQL & " AND timestamp <= '" & enddatetime  & "'"

response.write(SQL)

This way you just calculate the startdatetime and enddatetime parameters, and execute the same SQL for each case.

Please note that the way you write your SQL statements in ASP leaves you vulnerable to SQL injection attacks.

You might also want to consider writing date strings in ISO format (yyyy-mm-dd), that way the database will always understand the date. When you use cdate("1-6-2019"), this might be june first or january sixth, depending on how your database or OS is configured. When you use cdate("2019-6-1"), this is universally understood as june first.

Upvotes: 1

Related Questions