SQL Server中的别名

6vl6ewon  于 2023-01-20  发布在  SQL Server
关注(0)|答案(1)|浏览(140)
select eomonth('2023-01-01')

我希望函数MONTHNAME('2023-01-01')选取我的别名,这样输出将为2023-01-30,别名将为January

laawzig2

laawzig21#

您可以使用日期名:

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

动态别名:

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

添加您的查询:

declare @yourdate date = '2023-01-01'
 declare @monthname nvarchar(20)=(select DATENAME(MONTH,@yourdate))
 declare @anothersql nvarchar(500) = ',COALESCE((SELECT SUM(deductdays) FROM tbl_LeaveDeduction WHERE  CHARINDEX(quotename(CAST(EmpId AS varchar(10)),char(44)), @EmpIds) > 0 and  DATEPART(MONTH,DeductionDate)= DATEPART(MONTH,@StartofYear) ),0)'
 declare @SQL nvarchar(500) = concat('select eomonth(',quotename(@yourdate,char(39)),') as ',@monthname,@anothersql )
 exec sp_executesql @SQL

相关问题