3
I have the following appointment:
select case when datediff(...) > 0 then month(tblData.data1)
when day(tblData.data2) > 10 then month(tblData.data2)
when month(tblData.data2) = 1 then 12
else month(tblData.data2) - 1 end as Mês,
case when datediff(...) > 0 then year(tblData.data1)
when day(tblData.data2) > 10 then year(tblData.data2)
when month(tblData.data2) = 1 then year(tblData.data2) - 1
else year(tblData.data2) end as Ano,
concat(Ano, Mês) as AnoMes -- Como fazer essa linha aqui funcionar??
...
from where order by
I need the third column to concatenate the values of the previous two. However, if I put this function I take an undefined column name error since Ano
and Mês
are just nicknames.
- How can I concatenate these values avoiding the repetition of these
case when
???
Obs: the parts omitted in no way interfere with the question!
It is not possible to reference an alias in the same query, because this is processed at the end, and at the time of the "Month" execution it does not exist, it will only be at the end of the query. The solution to this is to transform the first case into a subselect
– Ricardo Pontual