获取设定年份的第一天

2021-09-10 00:00:00 sql tsql sql-server
declare @coyear varchar (max) 
set @coyear = '2018'
SELECT (YEAR(DATEADD(year,0,@coyear))) 

我已将 @coyear 设置为 2018 年,结果为 2018 年.我想要我为一年设置的一年中的第一天.例如,当我在 @coyear 中通过 2018 时,我希望结果为 2018-01-01.

I have set @coyear as 2018 and I am getting 2018 in result. I want first day of the year which I have set for year. For example when I pass 2018 in @coyear I want 2018-01-01 in result.

推荐答案

DATEFROMPARTS 似乎是更好的解决方案:

DATEFROMPARTS would seem a better solution:

DECLARE @CoYear int; --Why would you need 2GB/2 Billion characters for a year?
SET @CoYear = 2018;
SELECT DATEFROMPARTS(@CoYear, 1, 1);

相关文章