use Northwind
Go
CREATE FUNCTION getstar (@ datetime)
RETURNS varchar (MB)
as
BEGIN
--just one sentence SQL Handle
Return
(
--declare @ datetime
--set @ = getdate ()
Select Max (Star)
from
(
--Constellation, The constellation start date is the month that the constellation start date belongs to day
Select ' Capricorn ' as star,1 as [month],1 as [day]
UNION ALL select ' Aquarius ', 1,20
UNION ALL SELECT ' Pisces ', 2,19
Union ALL Select ' Aries ', 3,21
UNION ALL SELECT ' Taurus ', 4,20
UNION ALL SELECT ' Gemini ', 5,21
Union ALL Selec T ' cancer ', 6,22
Union ALL Select ' Leo ', 7,23
Union ALL select ' Virgo ', 8,23
Union ALL Select ' Libra ', 9,23
UNION ALL SEL ECT ' Scorpio ', 10,24
Union ALL select ' Sagittarius ', 11,22
Union ALL select ' Capricorn ', 12,22
) stars
where DATEADD (day,[day]-1 , DateAdd (Month,[month]-1,dateadd (Year,datediff (year,0,@), 0))
=
(
Select Max DateAdd (day,[day]-1, DATEADD (Month,[month]-1,dateadd (Year,datediff (year,0,@), 0)))
from
(
Select ' Capricorn ' as star,1 as [month],1 as] [Day]
UNION ALL Select ' Aquarius ', 1,20
Union ALL select ' Pisces ', 2,19
UNION ALL Select ' Aries ', 3,21
UNION ALL SELECT ' Taurus ', 4,20
UNION ALL SELECT ' Gemini ', 5,21
UNION ALL Select ' Cancer ', 6 ,
UNION ALL Select ' Leo ', 7,23
Union ALL select ' Virgo ', 8,23
Union ALL Select ' Libra ', 9,23
UNION ALL select ' Scorpio ' , 10,24
UNION ALL select ' Sagittarius ', 11,22
Union ALL select ' Capricorn ', 12,22
) stars
where @ >= DateAdd (day,[day]-1, DATEADD (Month,[month]-1,dateadd (Year,datediff (year,0,@), 0)) (
)
)
End
Go
--Test
use Northwind
Select Dbo.getstar (Birthdate), COUNT (*)
from Employees
Group by Dbo.getstar (birthdate)