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 坨,可以实现么?我在线等
2CL8:28(1533坨)
2CL10:4批(166坨)
2CL12:2批(70坨)
2CL14:0批(0坨)我想以上面记录作为表 从其中查询出批号 等于 2CL4 的 坨数 ,也就是查询出结果为 329 坨,可以实现么?我在线等
where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0
where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
-- 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 行受影响)
*/
SET @where='2CL4'
select dqx_q from # where [time] = '2011-09-01' AND dqx_q LIKE @where+':%'
where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0为什么没有查询出329坨呢
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#程序代码里呢
select dqx_q from storehouse
where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
where time = '2011-09-01' and charindex(dqx_q,'2CL4')>0怎么查询出的是空值呢
这是把你的数据放在SQL里来查.
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坨 这一个结果
FROM Storehouse
WHERE time = '2011-09-01'
和
SELECT dqx_q
FROM Storehouse
WHERE (time = '2011-09-01') AND (CHARINDEX('2CL4', dqx_q) > 0)
结果一样?
(
select dqx_q from storehouse
where time ='2011-09-01'
) as temp_tb
where left(dqx_q,4)='2CL4'
2CL4:4批(329坨)
2CL8:28(1533坨)
2CL10:4批(166坨)
2CL12:2批(70坨)
2CL14:0批(0坨)
这些数据啊!!!!
select CHARINDEX('2CL4', '2CL4:4批(329坨')
结果是多少?
where time ='2011-09-01' and left(dqx_q,4)='2CL4'
建议LZ看下charindex的用法再发表结论
那
select CHARINDEX('2CL4', '2CL8:28(1533坨')呢?
-- 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 行受影响)
*/
where time ='2011-09-01'查询出来,然后在dqx_q里根据不同的型号查询出不同的坨数来,就这个意思!
(@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'
(@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'
(
select dqx_q from storehouse
where time ='2011-09-01'
) as temp_tb
where left(dqx_q,4)='2CL4'这个可以查出 329坨,但是 该为2CL8怎么就不出结果了呢???有好办法没
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'
这是你想要的吗?
where time = '2011-09-01' and charindex('2CL4',dqx_q)>0
这段肯定没问题
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'
如果用以上方法也查不出来啊,不用存储过程
(
select dqx_q from storehouse
where time ='2011-09-01'
) as temp_tb
where charindex('2CL4',dqx_q)>0
修改了一下blackwolves
看行不行
(
select dqx_q from storehouse
where time ='2011-09-01'
) as temp_tb
where……
[/Quote]
查2CL8的坨数怎么查?
把charindex('2CL4',dqx_q)>0里面的'2CL4'换成2cl8不就行了?你所实在c#里的 那在那里面设置个变量存储查询条件,然后这个查询语句你肯定要写到sql的字符串变量里 在2cl4的位置换成你存储条件的那个变量不就行了?
(
select dqx_q from storehouse
where time ='2011-09-01'
) as temp_tb
having substring(dqx_q,0,charindex(':',dqx_q)+1)='2CL4'
借用的代码。
select t.dqx_q from (select dqx_q from storehouse where time = '2011-09-01') t where t.dqx_q = '2CL4'
这些记录都放在 time = '2011-09-01' 的dqx_q这个列名的一行一列中了
where charindex(rtrim(ltrim('2CL4')),dqx_q)>0这样写
消息 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 子句。
消息 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 子句。
不就是筛选出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查询出的都是第一个含有'(' 和')'的之间记录么?我说的对么
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
你这个只是判断 是否有 2CL8这个记录,也判断不出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只查询出了一条记录 329坨