SQL (SQLServer) 中函数前的连字符有什么用

What is the use of the hyphen before a function in SQL (SQLServer)

我想知道 SQL 语法背后的逻辑是什么,在调用括号中的 SQL 函数之前包含连字符。

这里是 SQL:

IF (@StartDate > @EndDate)
BEGIN
    SET @EndDate = @StartDate
    SET @StartDate = @EndDate
END

DECLARE @nonworkingweekdays int

--now deal with public holidays
SELECT @nonworkingweekdays = count("Date") from 
(
    select distinct
    (
    CASE datepart(weekday,date)
            WHEN 1 THEN null --ignore sundays   
            WHEN 7 THEN null --ignore saturdays
            else "Date"
    END
    ) AS "date" 
    from publicholidays
) nonworkingweekdays 
WHERE 
"Date" is not null and 
"Date" between @StartDate and DATEADD(day, -1, @EndDate)

RETURN
    CASE WHEN @StartDate <= @EndDate
    THEN
        dbo.FullWeekDays(@StartDate, @EndDate) - @nonworkingweekdays
    ELSE
        -(dbo.FullWeekDays(@StartDate, @EndDate) - @nonworkingweekdays)
    END 

我感到困惑的逻辑在 else 语句和此脚本底部的 return 语句中.

提前致谢:)

这是 TSQL Unary Negative Operator:

Returns the negative of the value of a numeric expression (a unary operator). Unary operators perform an operation on only one expression of any one of the data types of the numeric data type category.

连字符用于反转表达式值的符号,因为在这种情况下你有:

CASE WHEN @StartDate <= @EndDate
THEN
    dbo.FullWeekDays(@StartDate, @EndDate) - @nonworkingweekdays
ELSE
    -(dbo.FullWeekDays(@StartDate, @EndDate) - @nonworkingweekdays)
END 

您正在 return计算日期之间的差异减去 nonworkingdays 变量。

这在逻辑上是最好的选择,因为你不能有 -1 天的差异,你总是需要一个正整数的差异。

务必设法区分 return 的类型(当 startdate 大于 enddate 时,反之亦然)

是一元否定运算符,所以和(0 - <expression>)一样。

也就是说,我猜这可以更简单地表示为:

RETURN ABS(dbo.FullWeekDays(@StartDate, @EndDate) - @nonworkingweekdays)