Home > database >  Alias in SQL Server
Alias in SQL Server

Time:01-18

select eomonth('2023-01-01') 

I want my alias to be picked by the function MONTHNAME('2023-01-01') so my output would be 2023-01-30 and my alias would be January

CodePudding user response:

You can use DATENAME:

select eomonth('2023-01-01') ,DATENAME(MONTH, eomonth('2023-01-01') )

Dynamic alias:

declare @yourdate date = '2023-01-01'
declare @monthname nvarchar(20)=(select DATENAME(MONTH,@yourdate))
declare @SQL nvarchar(200) = concat('select eomonth(',quotename(@yourdate,char(39)),') as ',@monthname )
exec sp_executesql @SQL

enter image description here

  • Related