首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

刚才那个有关问题,如何取得指定字符‘/’前面的字符串

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

刚才那个问题,怎么取得指定字符‘/’前面的字符串。
表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 

热点排行