如何将Int转换为Varchar

时间:2016-07-26 09:37:53

标签: c# sql sql-server

我试图将Int转换为Varchar,但我无法获得输出。任何人都可以建议任何其他方式吗?

我有以下查询,需要将StoreNoint)投射到varchar

ALTER PROCEDURE [dbo].[getrevenue]
    @date1 DATE,
    @date2 DATE,
    @StoreNo NVARCHAR(max) 
AS
BEGIN
    DECLARE @sql_test NVARCHAR(max)

    --SET @StoreNo='68,78,104'
    SET @sql_test = 'SELECT t1.transtoreno AS StoreNO
            ,t3.NAME AS NAME
            ,t1.Dealdate AS DATE
            ,t1.UKEI AS UKEI
            ,t2.SubTotal AS SubTotal
            ,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
            ,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
            ,t2.ValueInquiries AS TotalDiscount
            ,t2.NetSale AS Netsale
            ,t2.TotalSale AS ToatlSale
            ,t2.Cash AS Cash
            ,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
            ,ISNULL(t2.Card, 0) AS Card
            ,ISNULl(t2.Suica, 0) AS Suica
            ,t2.WONPOINT AS WAONPOINT
            ,t1.TaxExemption AS TAXExemption
            ,t2.TaxTotal AS TaxTotal
            ,t2.Returngoods AS Returngoods
            ,t2.Regiminus AS RegiMinus
            ,t2.PrintRecipt AS printrecipt
            ,ISNULL(t1.Visitorcount, 0) AS VisitorCount
        FROM (
            SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
                ,(DealDate) AS Dealdate
                ,SUM(SalePrice) AS UKEI
                ,SUM(TansuNebikiPrice) AS twoeyesdicount
                ,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
                ,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
                ,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
                ,Sum(RegiMinusNo) AS Receiptissue
                ,SUM(VisitorCount) AS Visitorcount
            FROM POS_TtlTran
            GROUP BY StoreNo
                ,DealDate
            ) t1
        LEFT OUTER JOIN (
            SELECT DATE AS D
                ,cast(StoreNo AS NVARCHAR) AS s
                ,SUM(CASE 
                        WHEN SerialNo LIKE 23
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Cash
                ,SUM(CASE 
                        WHEN SerialNo LIKE 31
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Card
                ,SUM(CASE 
                        WHEN SerialNo LIKE 30
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS GiftVochuer
                ,SUM(CASE 
                        WHEN SerialNo LIKE 138
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Returngoods
                ,SUM(CASE 
                        WHEN SerialNo LIKE 160
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS PrintRecipt
                ,SUM(CASE 
                        WHEN SerialNo LIKE 304
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Suica
                ,SUM(CASE 
                        WHEN SerialNo LIKE 26
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS WONPOINT
                ,SUM(CASE 
                        WHEN SerialNo LIKE 139
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Regiminus
                ,SUM(CASE 
                        WHEN SerialNo LIKE 4
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS SubToTal
                ,SUM(CASE 
                        WHEN SerialNo LIKE 7
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS SubTotalDiscount
                ,SUM(CASE 
                        WHEN SerialNo LIKE 8
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TwoeyesubTotalDiscount
                ,SUM(CASE 
                        WHEN SerialNo LIKE 18
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS ValueInquiries
                ,SUM(CASE 
                        WHEN SerialNo LIKE 22
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TotalSale
                ,SUM(CASE 
                        WHEN SerialNo LIKE 114
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TaxTotal
                ,SUM(CASE 
                        WHEN SerialNo LIKE 2
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS NetSale
            FROM POS_FinTtl
            GROUP BY StoreNo
                ,DATE
            ) t2 ON t1.transtoreno = t2.s
            AND t1.Dealdate = t2.D
        LEFT OUTER JOIN (
            SELECT StoreNo AS No
                ,StoreName AS NAME
            FROM Store
            ) t3 ON t2.s = t3.No
        WHERE (
                t1.transtoreno IN (''' 
                + CAST(@StoreNo AS NVARCHAR(max)) + ''') 
                AND (t1.Dealdate between ''' + CAST(@date1 AS VARCHAR(30)) + ''' 
                AND ''' + CAST(@date2 AS VARCHAR(30)) + '''))'
END

2 个答案:

答案 0 :(得分:0)

where (t1.transtoreno IN (''' 
        + CAST(@StoreNo AS nvarchar(max)) + ''')

应该是

where (t1.transtoreno IN (''' 
        + replace(CAST(@StoreNo AS nvarchar(max)),',',''',''') + ''')

答案 1 :(得分:0)

希望这个会起作用

ALTER PROCEDURE [dbo].[getrevenue] @date1 DATE
    ,@date2 DATE
    ,@StoreNo NVARCHAR(max) `
AS
BEGIN
    DECLARE @sql_test NVARCHAR(max)

    --SET @StoreNo='68,78,104'
    SET @sql_test ='SELECT t1.transtoreno AS StoreNO
            ,t3.NAME AS NAME
            ,t1.Dealdate AS DATE
            ,t1.UKEI AS UKEI
            ,t2.SubTotal AS SubTotal
            ,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
            ,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
            ,t2.ValueInquiries AS TotalDiscount
            ,t2.NetSale AS Netsale
            ,t2.TotalSale AS ToatlSale
            ,t2.Cash AS Cash
            ,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
            ,ISNULL(t2.Card, 0) AS Card
            ,ISNULl(t2.Suica, 0) AS Suica
            ,t2.WONPOINT AS WAONPOINT
            ,t1.TaxExemption AS TAXExemption
            ,t2.TaxTotal AS TaxTotal
            ,t2.Returngoods AS Returngoods
            ,t2.Regiminus AS RegiMinus
            ,t2.PrintRecipt AS printrecipt
            ,ISNULL(t1.Visitorcount, 0) AS VisitorCount
        FROM (
            SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
                ,(DealDate) AS Dealdate
                ,SUM(SalePrice) AS UKEI
                ,SUM(TansuNebikiPrice) AS twoeyesdicount
                ,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
                ,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
                ,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
                ,Sum(RegiMinusNo) AS Receiptissue
                ,SUM(VisitorCount) AS Visitorcount
            FROM POS_TtlTran
            GROUP BY StoreNo
                ,DealDate
            ) t1
        LEFT OUTER JOIN (
            SELECT DATE AS D
                ,cast(StoreNo AS NVARCHAR) AS s
                ,SUM(CASE 
                        WHEN SerialNo LIKE 23
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Cash
                ,SUM(CASE 
                        WHEN SerialNo LIKE 31
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Card
                ,SUM(CASE 
                        WHEN SerialNo LIKE 30
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS GiftVochuer
                ,SUM(CASE 
                        WHEN SerialNo LIKE 138
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Returngoods
                ,SUM(CASE 
                        WHEN SerialNo LIKE 160
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS PrintRecipt
                ,SUM(CASE 
                        WHEN SerialNo LIKE 304
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Suica
                ,SUM(CASE 
                        WHEN SerialNo LIKE 26
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS WONPOINT
                ,SUM(CASE 
                        WHEN SerialNo LIKE 139
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS Regiminus
                ,SUM(CASE 
                        WHEN SerialNo LIKE 4
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS SubToTal
                ,SUM(CASE 
                        WHEN SerialNo LIKE 7
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS SubTotalDiscount
                ,SUM(CASE 
                        WHEN SerialNo LIKE 8
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TwoeyesubTotalDiscount
                ,SUM(CASE 
                        WHEN SerialNo LIKE 18
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS ValueInquiries
                ,SUM(CASE 
                        WHEN SerialNo LIKE 22
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TotalSale
                ,SUM(CASE 
                        WHEN SerialNo LIKE 114
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS TaxTotal
                ,SUM(CASE 
                        WHEN SerialNo LIKE 2
                            THEN DayTotalAmt
                        ELSE 0
                        END) AS NetSale
            FROM POS_FinTtl
            GROUP BY StoreNo
                ,DATE
            ) t2 ON t1.transtoreno = t2.s
            AND t1.Dealdate = t2.D
        LEFT OUTER JOIN (
            SELECT StoreNo AS No
                ,StoreName AS NAME
            FROM Store
            ) t3 ON t2.s = t3.No
        WHERE (
                t1.transtoreno IN (''' 
                + CONVERT(varchar(MAX), @StoreNo) + ''') 
                AND (t1.Dealdate between ''' + CAST(@date1 AS VARCHAR(30)) + ''' 
                AND ''' + CAST(@date2 AS VARCHAR(30)) + '''))'

END