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

刚才那个问题,怎么取得指定字符‘/’前面的字符串。
表T1 字符串类型字段F1  
如记录:F1 
  asdfasd/4855 
  weu50o/69521 
  NULL
  asjdlfjas

想得到结果是 
  asdfasd 
  weu50o
  NULL
  asjdlfjas

有的记录是空或不带‘/‘这个字符,不想用UNION查询

------解决方案--------------------
declare @s varchar(1000)

set @s ='asdfasd/4855'

select reverse(stuff(reverse(@s),1,charindex('/',reverse(@s)),''))
------解决方案--------------------

create table tab(context varchar(1000))
go
insert tab
select 'asdfasd/4855'
union all
select ' weu50o/69521'
union all
select null
union all
select 'asjdlfjas'
go

select rtrim(reverse(stuff(reverse(context),1,charindex('/',reverse(context)),'')))
from tab
go
drop table tab
------解决方案--------------------
SQL code

create table mytmp1
(
    text1 varchar(50)
)
insert into mytmp1
select 'sdfasd/4855' union all
select 'weu50o/69521  ' union all
select NULL union all
select 'asjdlfjas '

select text1 as 处理前,reverse(stuff(reverse(text1),1,charindex('/',reverse(text1)),'')) as 处理后
from mytmp1