question

arkiboys avatar image
0 Votes"
arkiboys asked VaibhavChaudhari answered

tsql date question

Hello,
Can you please correct the sql to get @ClosedMonthStart
at present I get the error: Explicit conversion from data type int to date is not allowed.
I would like @ClosedMonthStart to show '2021-05-01'
Thank you


DECLARE @PreviousYearOfCurrentDate date = dateadd(year, -1, current_timestamp)
DECLARE @PreviousYear INT = YEAR(@PreviousYearOfCurrentDate)
print @PreviousYear
DECLARE @ClosedMonthStart DATE = cast(@PreviousYear + '-05-01' as date)
--print @ClosedMonthStart

sql-server-transact-sql
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

VaibhavChaudhari avatar image
1 Vote"
VaibhavChaudhari answered

Does this work?

 DECLARE @PreviousYearOfCurrentDate date = dateadd(year, -1, current_timestamp)
 DECLARE @PreviousYear INT = YEAR(@PreviousYearOfCurrentDate)
 print @PreviousYear
    
 DECLARE @ClosedMonthStart DATE = cast(concat(@PreviousYear,'-05-01') as date)
 print @ClosedMonthStart


Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.