I would like to select the current year from the system and append '02/01' to it. I coded: SELECT CAST('02/01/' + YEAR(GETDATE()) as date) but it throws the error message:
Explicit conversion from data type int to date is not allowed.
Any help would be appreciated.
Answer by Kev Riley ·
That's because the + operator is an overloaded operator, meaning that it does different things depending on the usage.
You are trying to use it to do string concatenation, but because YEAR(GETDATE()) returns an int datatype, which has a higher datatype precedence than varchar, then SQL assumes you are using + to do integer addition. And so tries to add the string '02/01' to the integer 2021 - hence the error.
What you need to do is return the year as a varchar, and then SQL will happily concatenate the strings
SELECT CAST('02/01/' + cast(year(GETDATE())as varchar) as date)
SELECT CAST('02/01/' + datename(year,getdate()) as date)