Files
Fuchs_Intranet/Fuchs_Database/dbo/Functions/date_add.sql
T
StefanandClaude Fable 5.1 546648533b
Playwright Tests / test (push) Canceled after 0s
fix(drafts): serialize draft session flushes and register reminders via fds__setReminder
Make invoice/reminder live-draft edits concurrency-safe with an explicit
DraftFlushResult, add the fds__setReminder stored procedure, and harden the
date helper functions. Includes concurrency and SQL contract tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-01 13:57:40 +02:00

62 lines
2.2 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- INLINE = OFF: scalar UDF inlining of this IF/nested-UDF body (-> AddBusinessDays)
-- killed the dashboard widget queries with error 596 ("session is in the kill
-- state", class 21). Called with constant arguments, so per-row cost is negligible.
-- =============================================
CREATE FUNCTION [dbo].[date_add]
(
@date date
,@addition varchar(10)
,@shift_to_first bit = 1
)
RETURNS date
WITH INLINE = OFF
AS
BEGIN
DECLARE @Ret date = @date;
SET @addition = LOWER(ISNULL(@addition,''));
DECLARE @negative bit = 0;
IF LEFT(@addition,1) = '-'
BEGIN
SET @negative = 1;
SET @addition = REPLACE(@addition, '-', '');
END
IF @addition like '[1-9]%[dwmqy]'
BEGIN
DECLARE @num int = TRY_CONVERT(int, LEFT(@addition, patindex('%[^0-9]%', @addition) - 1)) * IIF(@negative = 1, -1, 1), @unit varchar(2) = LOWER(RIGHT(@addition, LEN(@addition) - patindex('%[^0-9]%', @addition) + 1)) ;
if @num is not null
BEGIN
SET @ret = CASE WHEN ISNULL(@shift_to_first,1) = 1 THEN
CASE
WHEN @unit = 'y' THEN DATEADD(YEAR, @num - 1, dbo.date_nextFirst(@date, 'y'))
WHEN @unit = 'q' THEN DATEADD(QUARTER, @num - 1, dbo.date_nextFirst(@date, 'q'))
WHEN @unit = 'm' THEN DATEADD(Month, @num - 1, dbo.date_nextFirst(@date, 'm'))
WHEN @unit = 'w' THEN DATEADD(WEEK, @num - 1, dbo.date_nextFirst(@date, 'w'))
WHEN @unit = 'd' THEN DATEADD(DAY, @num - 1, dbo.date_nextFirst(@date, 'd'))
WHEN @unit in ('bd','wd') THEN [dbo].[AddBusinessDays](@date, @num)
ELSE @date
END
ELSE
CASE
WHEN @unit = 'y' THEN DATEADD(YEAR, @num, @date)
WHEN @unit = 'q' THEN DATEADD(QUARTER, @num, @date)
WHEN @unit = 'm' THEN DATEADD(Month, @num, @date)
WHEN @unit = 'w' THEN DATEADD(WEEK, @num, @date)
WHEN @unit = 'd' THEN DATEADD(DAY, @num, @date)
WHEN @unit in ('bd','wd') THEN [dbo].[AddBusinessDays](@date, @num)
ELSE @date
END
END;
END
END
RETURN @ret;
END