在sql server中将Concat字符串转换为Int .-但是收到错误

时间:2016-06-24 07:10:44

标签: sql sql-server

当我尝试通过此检索数据时,它可以正常工作。

SELECT 
CONVERT(VARCHAR(10),BH.Doc_Date,(120))AS Doc_Date
,LGH.Node_Code
,LGH.Node_Name AS Location_Name
,GD.Nationality
,GD.Guest_Name
,DBOGD.Name AS Gender
,CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + ' yrs' AS Age
,'++' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
,GD.NIC_No
,GD.Language
FROM [booking].[Guest_Details] AS GD ...

但是当我想这样做的时候,

SET @query = 'SELECT 
                CONVERT(VARCHAR(10)
                ,BH.Doc_Date,(120))AS Doc_Date
                ,LGH.Node_Code
                ,LGH.Node_Name AS Location_Name
                ,GD.Nationality
                ,GD.Guest_Name
                ,DBOGD.Name AS Gender
                ,(CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + " yrs") AS Age
                ,"++" + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
                ,GD.NIC_No
                ,GD.Language
                FROM [booking].[Guest_Details] AS GD ...'

它不起作用。我得到的错误如下:

  

列名'yrs'无效。

     

列名'++'无效。

3 个答案:

答案 0 :(得分:2)

Yrs部分导致您发出..如下所示修改

com.test.comp:comp-app-common:3.0.10_SNAPSHOT

答案 1 :(得分:1)

试试这段代码,希望这可以帮到你。

SET @query = 'SELECT 
                CONVERT(VARCHAR(10)
                ,BH.Doc_Date,(120))AS Doc_Date
                ,LGH.Node_Code
                ,LGH.Node_Name AS Location_Name
                ,GD.Nationality
                ,GD.Guest_Name
                ,DBOGD.Name AS Gender
                ,(CAST(DATEDIFF(yy,GD.Birth_Day,getdate()) AS NVARCHAR(100)) + '' yrs'') AS Age
                ,''++'' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile
                ,GD.NIC_No
                ,GD.Language
                FROM [booking].[Guest_Details] AS GD ...'

答案 2 :(得分:1)

而不是双引号",请在这些地方使用两个单引号'' " yrs""++"

SET @query = 
    'SELECT 
    CONVERT(VARCHAR(10), BH.Doc_Date,(120)) AS Doc_Date
    ,LGH.Node_Code
    ,LGH.Node_Name AS Location_Name
    ,GD.Nationality
    ,GD.Guest_Name
    ,DBOGD.Name AS Gender
    ,(CAST(DATEDIFF(yy, GD.Birth_Day,getdate()) AS NVARCHAR(100)) + '' yrs'') AS Age -- in this line
    ,''++'' + CAST(GD.Mobile AS VARCHAR(100)) AS Mobile -- and in this line
    ,GD.NIC_No
    ,GD.Language
    FROM [booking].[Guest_Details] AS GD ...'