基于输入的日期在VBScript中进行SQL查询

基于输入的日期在VBScript中进行SQL查询,vbscript,asp-classic,Vbscript,Asp Classic,我有一个VBscript,可以查询数据库以提取基于班次的数据,例如墓地、白天和秋千。我只需要在2019年1月6日之后的几天内将时间缩短一小时 我尝试过的解决方案是扩展if语句并添加and函数,但它不起作用,因为第一个if语句仍然为true dim intCoilCount, intTotalSeconds,intSeconds,strDate,strShift 'SQL="select timeStamp, coil_number, entry_gaptime,thickness,width_

我有一个VBscript,可以查询数据库以提取基于班次的数据,例如墓地、白天和秋千。我只需要在2019年1月6日之后的几天内将时间缩短一小时

我尝试过的解决方案是扩展if语句并添加and函数,但它不起作用,因为第一个if语句仍然为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

我会在SQL字符串中保留那个讨厌的逻辑,并在vbscript中这样做。类似这样的未经测试:

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)
这样,您只需计算startdatetime和enddatetime参数,并对每种情况执行相同的SQL

请注意,在ASP中编写SQL语句的方式容易受到攻击


您可能还想考虑在ISO格式YYYY MM DD中写入日期字符串,这样数据库将永远了解日期。使用cdate1-6-2019时,这可能是6月1日或1月6日,具体取决于数据库或操作系统的配置方式。当您使用cdate2019-6-1时,这被普遍理解为六月一日。

也将日期条件添加到第一个if语句中,如果strShift=graveyard和CDatestrDate