Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#T-SQL, 296 bytes

T-SQL, 296 bytes

#T-SQL, 296 bytes

T-SQL, 296 bytes

Source Link
MickyT
  • 12.3k
  • 2
  • 27
  • 49

#T-SQL, 296 bytes

Created as a table valued function

create function d(@ char(5))returns table return select min(o)o from(select datediff(day,cast('2016'+right(@,2)+left(@,2)as date),cast(right('2016'+right('0'+cast(d as varchar(4)),4),8)as datetime)+1)o from(values(324),(325),(327),(424),(501),(1002),(1224),(1225),(1226),(1231))d(d))d where 0<=o

Used in the following manner

SELECT *
FROM (
 VALUES
 ('01/05') --= 1 
 ,('02/05') --= 0 
 ,('03/05') --= 153 
 ,('25/12') --= 0
 ,('26/12') --= 0
 ,('27/12') --= 0
 ,('30/12') --= 2
 ,('31/12') --= 1
 )testData(i)
 CROSS APPLY (
 SELECT * FROM d(t)
 ) out
i o
----- -----------
01/05 1
02/05 0
03/05 153
25/12 0
26/12 0
27/12 0
30/12 2
31/12 1
(8 row(s) affected)

A brief explanation

create function d(@ char(5)) returns table -- function definition
return 
select min(o)o -- minimum set value
from(
 select datediff( -- date difference
 day, -- day units
 cast('2016'+right(@,2)+left(@,2)as date), -- convert input parameter to date
 cast(right('2016'+right('0'+cast(d as varchar(4)),4),8)as datetime)+1 -- convert int values into datetimes and add a day
 )o 
 from(
 values(324),(325),(327),(424),(501),(1002),(1224),(1225),(1226),(1231) -- integers representing the day before public holidays
 )d(d)
 )d 
where 0<=o -- only for values >= 0

AltStyle によって変換されたページ (->オリジナル) /