如何编写一个 sybase sql anywhere 函数来检查一天中的时间是否在开始时间和结束时间之间
How to write a sybase sql anywhere function that checks if time of day is between start time and end time
我想在 Sybase 中编写一个函数 IsTimeOfDayBetween SQL 如果时间在开始日期时间和结束日期时间之间,则 return 为真,否则为假。
我不知道该怎么做 - 我试过了:
DECLARE @isTimeInTimeInterval BIT;
set @isTimeInTimeInterval = (CAST(@startTime as time) > '09:00:00' and CAST(@startTime as time) < '18:00:00');
然而它不起作用。
如有任何建议,我们将不胜感激。
答案如下:
CREATE FUNCTION "DBA"."IsTimeOfDayBetween"(timeOfDay DateTime, startTime DateTime, endTime DateTime )
RETURNS BIT
DETERMINISTIC
BEGIN
DECLARE "Result" BIT;
DECLARE bvrTime TIME;
DECLARE bvrStartTime TIME;
DECLARE bvrEndTime TIME;
SET bvrTime = timeOfDay;
SET bvrStartTime = startTime;
SET bvrEndTime= endTime;
if bvrStartTime = bvrEndTime then
set "Result" = 1;
end if;
if bvrEndTime < bvrStartTime then
if ((bvrTime <= bvrEndTime) or (bvrTime >= bvrStartTime)) then
set "Result" = 1
else set "Result" = 0
end if;
end if;
if ((bvrTime >= bvrStartTime) and(bvrTime <= bvrEndTime)) then
set "Result" = 1;
else set "Result" = 0;
end if;
RETURN "Result";
END
基于此,将 DateTime 变量赋值给 Time 变量时,最后得到的只是时间部分。
干杯!
我想在 Sybase 中编写一个函数 IsTimeOfDayBetween SQL 如果时间在开始日期时间和结束日期时间之间,则 return 为真,否则为假。
我不知道该怎么做 - 我试过了:
DECLARE @isTimeInTimeInterval BIT;
set @isTimeInTimeInterval = (CAST(@startTime as time) > '09:00:00' and CAST(@startTime as time) < '18:00:00');
然而它不起作用。
如有任何建议,我们将不胜感激。
答案如下:
CREATE FUNCTION "DBA"."IsTimeOfDayBetween"(timeOfDay DateTime, startTime DateTime, endTime DateTime )
RETURNS BIT
DETERMINISTIC
BEGIN
DECLARE "Result" BIT;
DECLARE bvrTime TIME;
DECLARE bvrStartTime TIME;
DECLARE bvrEndTime TIME;
SET bvrTime = timeOfDay;
SET bvrStartTime = startTime;
SET bvrEndTime= endTime;
if bvrStartTime = bvrEndTime then
set "Result" = 1;
end if;
if bvrEndTime < bvrStartTime then
if ((bvrTime <= bvrEndTime) or (bvrTime >= bvrStartTime)) then
set "Result" = 1
else set "Result" = 0
end if;
end if;
if ((bvrTime >= bvrStartTime) and(bvrTime <= bvrEndTime)) then
set "Result" = 1;
else set "Result" = 0;
end if;
RETURN "Result";
END
基于此,将 DateTime 变量赋值给 Time 变量时,最后得到的只是时间部分。
干杯!