select dqx_q from storehouse where time = '2011-09-01'通过上面语句查询出  以下记录2CL4:4批(329坨)
2CL8:28(1533坨)
2CL10:4批(166坨)
2CL12:2批(70坨)
2CL14:0批(0坨)我想以上面记录作为表 从其中查询出批号 等于 2CL4 的 坨数 ,也就是查询出结果为 329 坨,可以实现么?我在线等

解决方案 »

  1.   

    select dqx_q from storehouse 
    where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0
      

  2.   

    select dqx_q from storehouse where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0
      

  3.   

    select dqx_q from storehouse 
    where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
      

  4.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-09-05 10:27:04
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) 
    -- Apr  2 2010 15:53:02 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(16))
    insert [tb]
    select '2CL4:4批(329坨)' union all
    select '2CL8:28(1533坨)' union all
    select '2CL10:4批(166坨)' union all
    select '2CL12:2批(70坨)' union all
    select '2CL14:0批(0坨)'
    --------------开始查询--------------------------
    select * from tb where charindex('2CL4',col)>0
    ----------------结果----------------------------
    /* col
    ----------------
    2CL4:4批(329坨)(1 行受影响)
    */
      

  5.   

    charindex('2CL4',dqx_q)>0怎么没起作用呢
      

  6.   

    DECLARE @where NVARCHAR(20)
    SET @where='2CL4'
    select dqx_q from # where [time] = '2011-09-01' AND dqx_q LIKE @where+':%'
      

  7.   

    select dqx_q from storehouse 
    where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0为什么没有查询出329坨呢
      

  8.   

    4楼的我没明白
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(16))
    insert [tb]
    select '2CL4:4批(329坨)' union all
    select '2CL8:28(1533坨)' union all
    select '2CL10:4批(166坨)' union all
    select '2CL12:2批(70坨)' union all
    select '2CL14:0批(0坨)'你这些创建表的东东,我要怎么写在c#程序代码里呢
      

  9.   


    select dqx_q from storehouse  
    where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
      

  10.   

    select dqx_q from storehouse
     
    where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0怎么查询出的是空值呢
      

  11.   


    这是把你的数据放在SQL里来查.
      

  12.   

    SELECT     dqx_q
    FROM         Storehouse
    WHERE     (time = '2011-09-01') AND (CHARINDEX('2CL4', dqx_q) > 0)这样查出了2CL4:4批(329坨)
    2CL8:28(1533坨)
    2CL10:4批(166坨)
    2CL12:2批(70坨)
    2CL14:0批(0坨)这些结果,我就要  329坨   这一个结果
      

  13.   

    你这句话不太靠谱,自己看看charindex语法,可能是这个结果么?
      

  14.   

    SELECT dqx_q
    FROM Storehouse
    WHERE time = '2011-09-01'

    SELECT dqx_q
    FROM Storehouse
    WHERE (time = '2011-09-01') AND (CHARINDEX('2CL4', dqx_q) > 0)
    结果一样?
      

  15.   

    不知道楼主的SQL版本,以下是SQL2005代码select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
    select dqx_q from storehouse 
    where time ='2011-09-01'
    ) as temp_tb
    where left(dqx_q,4)='2CL4' 
      

  16.   

    我是dqx_q这一个字段里就有 where time = '2011-9-1'
    2CL4:4批(329坨)
    2CL8:28(1533坨)
    2CL10:4批(166坨)
    2CL12:2批(70坨)
    2CL14:0批(0坨)
    这些数据啊!!!!
      

  17.   


    select CHARINDEX('2CL4', '2CL4:4批(329坨')
    结果是多少?
      

  18.   

    select dqx_q from storehouse 
        where time ='2011-09-01' and left(dqx_q,4)='2CL4'
      

  19.   

    ...
    建议LZ看下charindex的用法再发表结论
      

  20.   



    select CHARINDEX('2CL4', '2CL8:28(1533坨')呢?
      

  21.   

    总不能把所有对应的型号 的sql 语句都写在后台吧
      

  22.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-09-05 10:27:04
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) 
    --    Apr  2 2010 15:53:02 
    --    Copyright (c) Microsoft Corporation
    --    Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(16))
    insert [tb]
    select '2CL4:4批(329坨)' union all
    select '2CL8:28(1533坨)' union all
    select '2CL10:4批(166坨)' union all
    select '2CL12:2批(70坨)' union all
    select '2CL14:0批(0坨)'
    --------------开始查询--------------------------
    select substring(col,charindex('(',col)+1,charindex(')',col)-charindex('(',col)-1) from tb where charindex('2CL4',col)>0
    ----------------结果----------------------------
    /*----------------
    329坨(1 行受影响)
    */
      

  23.   

    33楼,我不明白你为什么总要我建临时表呢,我有我自己的数据库结构,如果按照你的写法,我又要重新建立新表,而且数据都是固定的,这在实际当中是没有意义的,因为数据是变化的,你的表还得重新建立新表,我是求sql语句,不是求新表,谢谢!
      

  24.   

    我在重新说一遍,我的数据都在dqx_q这一个字段里,可以通过select dqx_q from storehouse 
        where time ='2011-09-01'查询出来,然后在dqx_q里根据不同的型号查询出不同的坨数来,就这个意思!
      

  25.   

    我那个是测试数据 你要的只是下面的SQL语句 
      

  26.   

    create proc test
    (@xh varchar(10))
    begin
    select
     substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
     (select dqx_q from storehouse where time ='2011-09-01') as temp_tb
    where
     left(dqx_q,4)=@xh
    endexec  test '2CL4'
      

  27.   

    掉了个 ascreate proc test
    (@xh varchar(10))
    as
    begin
    select
     substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
     (select dqx_q from storehouse where time ='2011-09-01') as temp_tb
    where
     left(dqx_q,4)=@xh
    endexec  test '2CL4'
      

  28.   

    直接写sql 语句能查不,然我参数由我来输入
      

  29.   

    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
        select dqx_q from storehouse 
        where time ='2011-09-01'
    ) as temp_tb
    where left(dqx_q,4)='2CL4'这个可以查出 329坨,但是 该为2CL8怎么就不出结果了呢???有好办法没
      

  30.   

    select dqx_q from storehouse where time = '2011-09-01'通过上面语句查询出 以下记录2CL4:4批(329坨)
    2CL8:28(1533坨)
    2CL10:4批(166坨)
    2CL12:2批(70坨)
    2CL14:0批(0坨)select a.dqx_q from (select dqx_q from storehouse where time = '2011-09-01'
    ) a where a.dqx_q ='2CL4'
    这是你想要的吗?
      

  31.   

    根据不同型号在dqx_q这个字段内查询出相应的坨数
      

  32.   

    select dqx_q from storehouse 
    where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
    这段肯定没问题
      

  33.   

    无法查询出某个型号的坨数,结果返回了dqx_q中的所有记录
      

  34.   

    我的数据都在dqx_q这个字段里,我用
    select dqx_q from storehouse where time = '2011-09-01' 条语句查询出了
    2CL4:4批(329坨)2CL8:28(1533坨)2CL10:4批(166坨)2CL12:2批(70坨)2CL14:0批(0坨)
    这些数据,加入型号改变时,比如输入2CL4或者2CL8时,查询出相应的坨数
    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
        select dqx_q from storehouse 
        where time ='2011-09-01'
    ) as temp_tb
    where left(dqx_q,4)='2CL4'这个可以查出  329坨,假如查2CL8的坨数怎么办
    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
        select dqx_q from storehouse 
        where time ='2011-09-01'
    ) as temp_tb
    where left(dqx_q,4)='2CL8'
    如果用以上方法也查不出来啊,不用存储过程
      

  35.   

    小f姐已经疯了。我感觉select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
        select dqx_q from storehouse 
        where time ='2011-09-01'
    ) as temp_tb
    where charindex('2CL4',dqx_q)>0
    修改了一下blackwolves
    看行不行
      

  36.   

    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
      select dqx_q from storehouse 
      where time ='2011-09-01'
    ) as temp_tb
    where……
    [/Quote]
    查2CL8的坨数怎么查?
      

  37.   


    把charindex('2CL4',dqx_q)>0里面的'2CL4'换成2cl8不就行了?你所实在c#里的  那在那里面设置个变量存储查询条件,然后这个查询语句你肯定要写到sql的字符串变量里 在2cl4的位置换成你存储条件的那个变量不就行了?
      

  38.   

    我换了啊,但是sql 2005 查不出结果呢
      

  39.   

    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
        select dqx_q from storehouse 
        where time ='2011-09-01'
    ) as temp_tb
    having substring(dqx_q,0,charindex(':',dqx_q)+1)='2CL4'
    借用的代码。
      

  40.   

    不会的啊 我测试可以查出来啊 你看看是不是那个条件变量里的2cl8左右又空格 比如 如果是  '空格2CL8'那就是空了
      

  41.   

    这些记录都放在 time = '2011-09-01' 的dqx_q这个列名的一行一列中了
      

  42.   


    select t.dqx_q from (select dqx_q from storehouse where time = '2011-09-01') t where t.dqx_q = '2CL4'
      

  43.   

    2CL4:4批(329坨)2CL8:28(1533坨)2CL10:4批(166坨)2CL12:2批(70坨)2CL14:0批(0坨)
    这些记录都放在 time = '2011-09-01' 的dqx_q这个列名的一行一列中了
      

  44.   


    where charindex(rtrim(ltrim('2CL4')),dqx_q)>0这样写
      

  45.   

    这怎么可能 你先把里面的2CL4换成2CL8 看看是不是一样的  如果不一样那就是你这个条件变量获取的有问题的  
      

  46.   

    sql server2005重启了,换成2CL8查出得结果还是329坨
      

  47.   

    有错误啊
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8119,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在 HAVING 子句中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8119,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在 HAVING 子句中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
      

  48.   

    有错误啊
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8118,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8119,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在 HAVING 子句中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    消息 8119,级别 16,状态 1,第 1 行
    列 'temp_tb.dqx_q' 在 HAVING 子句中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
      

  49.   

    你这句的意思where charindex(rtrim(ltrim('2CL8')),dqx_q)>0
    不就是筛选出dqx_q中 是否含有2CL8这个字符串么,
    但是你这些
    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from  
    (
      select dqx_q from storehouse  
      where time ='2011-09-01'
    ) as temp_tb查询出的都是第一个含有'(' 和')'的之间记录么?我说的对么
      

  50.   


    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from 
    (
    select dqx_q from storehouse 
    where time='2011-09-01'
    ) as temp_tb1
    where left(dqx_q,charindex(':',dqx_q)-1)=strType --'2CL8'strType是C#代码里面你要传的进来的型号参数,可以是2CL4、2CL8、2CL14等等
    如果是同时查多个型号的话,就用in
      

  51.   

    where charindex(rtrim(ltrim('2CL8')),dqx_q)>0
    你这个只是判断 是否有 2CL8这个记录,也判断不出2CL8的起始位置啊
      

  52.   

    没看见把查出来的做表temp_tb了吗 然后根据where赛选出temp_tb里面符合条件的记录 再通过select选出来啊  并不是第一个,那你试试在每个dqx_q前面加个temp_tb.  不过我感觉意义不大你判断他的起始位干嘛 只要判断数据存在就行了啊
      

  53.   

    但是这些
    select substring(dqx_q,charindex('(',dqx_q)+1,charindex(')',dqx_q)-charindex('(',dqx_q)-1) from  
    (
      select dqx_q from storehouse  
      where time ='2011-09-01'
    ) as temp_tb只查询出了一条记录 329坨