读书人

刚才那个有关问题如何取得指定字符‘

发布时间: 2012-02-01 16:58:19 作者: rapoo

刚才那个问题,怎么取得指定字符‘/’前面的字符串。
表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 mytmp1select 'sdfasd/4855' union allselect 'weu50o/69521  ' union allselect NULL union allselect 'asjdlfjas 'select text1 as 处理前,reverse(stuff(reverse(text1),1,charindex('/',reverse(text1)),'')) as 处理后from mytmp1 

读书人网 >SQL Server

热点推荐