SQL 根据输入的日期在 VBScript 中查询
SQL Query in VBScript based on the date entered
我有一个 VBscript 查询数据库以提取基于班次的数据,例如墓地、白天和秋千。我只需要在 1-6-2019 之后的日子里将时间减少一个小时。
我尝试过的解决方案是扩展我的 if 语句并添加一个 AND 函数,但它不起作用,因为第一个 if 语句仍然为真。
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 语句的方式使您容易受到 SQL injection attacks 的攻击。
您可能还想考虑以 ISO 格式 (yyyy-mm-dd) 编写日期字符串,这样数据库将始终理解日期。当您使用 cdate("1-6-2019")
时,这可能是六月一日或一月六日,具体取决于您的数据库或 OS 的配置方式。当您使用 cdate("2019-6-1")
时,这通常被理解为六月一日。
我有一个 VBscript 查询数据库以提取基于班次的数据,例如墓地、白天和秋千。我只需要在 1-6-2019 之后的日子里将时间减少一个小时。
我尝试过的解决方案是扩展我的 if 语句并添加一个 AND 函数,但它不起作用,因为第一个 if 语句仍然为真。
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 语句的方式使您容易受到 SQL injection attacks 的攻击。
您可能还想考虑以 ISO 格式 (yyyy-mm-dd) 编写日期字符串,这样数据库将始终理解日期。当您使用 cdate("1-6-2019")
时,这可能是六月一日或一月六日,具体取决于您的数据库或 OS 的配置方式。当您使用 cdate("2019-6-1")
时,这通常被理解为六月一日。