日期:2014-05-18  浏览次数:20619 次

如何将字段中的值分段?
如果某个字段中的值的格式为X*Y,保存在字段中是以字符串类型保存的

在查询时如何将这个字段值分段,可以直接查询得到X和Y


如:

字段名           SpecValue
字段值           10000*1800
                      8000*1500
                      2000*12000
.
.
.

查询后需要的值

字段名               L                               W
字段值           10000                       1800
                        8000                       1500
                        2000                     12000


------解决方案--------------------
select left(SpecValue,charindex( '* ',SpecValue) - 1) x , substring(SpecValue , charindex( '* ',SpecValue) + 1 , len(SpecValue)) y from tb
------解决方案--------------------
比如取x> 2000

create table tb(SpecValue varchar(20))
insert into tb values( '10000*1800 ')
insert into tb values( '8000*1500 ')
insert into tb values( '2000*12000 ')
go
select SpecValue , cast(left(SpecValue,charindex( '* ',SpecValue) - 1) as int) x, cast(substring(SpecValue , charindex( '* ',SpecValue) + 1 , len(SpecValue) - charindex( '* ',SpecValue)) as int) y from tb where cast(left(SpecValue,charindex( '* ',SpecValue) - 1) as int) > 2000

drop table tb

/*
SpecValue x y
-------------------- ----------- -----------
10000*1800 10000 1800
8000*1500 8000 1500

(所影响的行数为 2 行)
*/