现在库中的数据是这样存储的,比如作者有一个或多个,多个之间我用%分隔的,一个的话就没有%,我现在想取出第一个应该怎么取呢? 比如“李明%张平%郭大可”我想取出李明,如果只有一个李明就全取了,谢谢大家!!!!
------解决方案--------------------
- SQL code
select substring(col,1,charindex('%',col+'%')-1) from tb--没环境,没测,LZ试下
------解决方案--------------------
- SQL code
--或者select left(col,charindex('%',col+'%')-1) from tb--col换成你的字段
------解决方案--------------------
- SQL code
declare @t table (col varchar(16))insert into @tselect '李明%张平%郭大可' union allselect '张平%郭大可' union allselect '郭大可'select col=left(col,charindex('%',col+'%')-1) from @t/*col----------------李明张平郭大可*/
------解决方案--------------------
- SQL code
declare @t table (col varchar(16))insert into @tselect '李明%张平%郭大可' union allselect '张平%郭大可' union allselect '郭大可'--如果%不超过3个,可以用parsenameselect col=parsename(replace(col,'%','.'),len(col)-len(replace(col,'%',''))+1) from @t/*col----------李明张平郭大可*/