SQL查询和concat不起作用,错误抛出每个更改

时间:2016-09-14 15:37:02

标签: sql-server sql-server-2008

我有以下查询,但串联不起作用,

Select Distinct DateName(month ,DateAdd(month , Month(aih.ddl) , 0) - 1) as sMonth+'-'+
(year(aih.ddl)) as syear 
FROM ail
inner join aipd on aipd.ID = aih.OfferingID 
Where aih.typeIN ('A') AND aipd.pid= 5034

1 个答案:

答案 0 :(得分:1)

DateName(month ,DateAdd(month , Month(aih.ddl) , 0) - 1) as sMonth+'-'+
(year(aih.ddl)) as syear 

您的代码几乎没有问题..

DateName(month ,DateAdd(month , Month(aih.ddl) , 0) - 1)是varchar
(year(aih.ddl))将是int -也是varchar

cast(year(somevalue) as varchar(4))以避免错误。

正如亚历克斯在下面的评论中提到的,你需要提供最终别名,而不是中间别名..而是连接

DateName(month ,DateAdd(month , Month(aih.ddl) , 0) - 1) +'-'+
(year(aih.ddl)) as 'column name you want to choose'