I have tried the queries below to check whether the current year is Leap-Year or not.
Declare @leapyearstrt int = 0
--1st query
select @leapyearstrt = ISNULL(ISLEAPYEAR,0)
FROM [dbo].[TimeElement]
WHERE CurrentDate = getdate()
--2nd query
SET @leapyearstrt = (SELECT isnull(ISLEAPYEAR,0)
FROM [dbo].[TimeElement]
WHERE CurrentDate = getdate()
)
If the data does not exist in the table for the given date, 1st query returns @leapyearstrt
as 0
, but 2nd query returns @leapyearstrt
as NULL
.
Why does it happen? How to assign the 0
value in the SET
statement if data does not exist in the table?
(P.S : I have used SET
to assign the value because select
may return more than one row)