SQLserver

select abstractInfo, substring(brandName,1,charindex('-',brandName)-1) brand,brandname,sum(num) total from (

 select id, brandname 

,brandBatch,status,abstractInfo,inStock,nowStock,outStock,createDateTime,cargoSpace,

(case when abstractInfo='入库' then  inStock    

     when abstractInfo='出库' then outStock  

     else '' end)   num

from dbo.inOutStockInfo where  abstractInfo='${type}' and status <>'营销物资' and  

 datediff(year,createDateTime,getdate())=0 ) a

 group by   abstractInfo,substring(brandName,1,charindex('-',brandName)-1),brandname

image.png

把charindex('-',brandName)-1  改为10,OK

SQLserver 是否对 substring 函数长度做限制?

substring(brandName,1,charindex('-',brandName)-1)  如何减少嵌套层数?

FineReport yzm128012 发布于 2023-9-11 09:55 (编辑于 2023-9-11 10:07)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
0
Z4u3z1Lv6专家互助
发布于2023-9-11 09:56(编辑于 2023-9-11 10:04)

有什么问题?

-----------

brandName字段中有些值没有 "-" 这个符号 就变成了 substring(brandName,1,0-1) 第三个参数就变成了负数 因此报错

---------

select abstractInfo, (case when charindex('-',brandName)>0 then substring(brandName,1,charindex('-',brandName)-1) else brandName end ) brand,brandname,sum(num) total from (

 select id, brandname 

,brandBatch,status,abstractInfo,inStock,nowStock,outStock,createDateTime,cargoSpace,

(case when abstractInfo='入库' then  inStock    

     when abstractInfo='出库' then outStock  

     else '' end)   num

from dbo.inOutStockInfo where  abstractInfo='${type}' and status <>'营销物资' and  

 datediff(year,createDateTime,getdate())=0 ) a

 group by   abstractInfo,brandname

最佳回答
0
congerLv6高级互助
发布于2023-9-11 09:57
  • 2关注人数
  • 163浏览人数
  • 最后回答于:2023-9-11 10:07
    请选择关闭问题的原因
    确定 取消
    返回顶部