gpt4 book ai didi

sql-server - 数据类型 varchar 和 date 在 add 运算符中不兼容

转载 作者:行者123 更新时间:2023-12-05 01:01:55 31 4
gpt4 key购买 nike

在执行这个查询时,我得到一个错误,

第 102 行的 add 运算符中的数据类型 varchar 和 date 不兼容。您能帮忙吗?

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,
t2.SubTotalDiscount AS SubToatlDiscount,
t1.twoeyesSubtotalDiscount As TwoeyeSubTotalDiscount,
t2.ValueInquiries AS TotalDiscount,
t2.NetSale AS Netsale,
t2.TotalSale AS ToatlSale,
t2.Cash As Cash,
t2.GiftVochuer As GiftVochuer,
t2.Card AS Card,
t2.Suica 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,
t1.Visitorcount 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, StoreNo 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 VARCHAR(100)) + ') and (t1.Dealdate between ' + CAST(@date1 AS DATE) + ' and ' + CAST(@date2 AS DATE) + ')'
END

最佳答案

您应该在连接之前将日期转换为 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,
t2.SubTotalDiscount AS SubToatlDiscount,
t1.twoeyesSubtotalDiscount As TwoeyeSubTotalDiscount,
t2.ValueInquiries AS TotalDiscount,
t2.NetSale AS Netsale,
t2.TotalSale AS ToatlSale,
t2.Cash As Cash,
t2.GiftVochuer As GiftVochuer,
t2.Card AS Card,
t2.Suica 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,
t1.Visitorcount 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, StoreNo 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 VARCHAR(100)) + ''') and (t1.Dealdate between ''' + CAST(@date1 AS VARCHAR(30)) + ''' and ''' + CAST(@date2 AS VARCHAR(30)) + ''')'

EXEC SP_EXECUTESQL @sql_test
END

关于sql-server - 数据类型 varchar 和 date 在 add 运算符中不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38501825/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com