简体   繁体   中英

Removing characters after numbers in a column in SQL Server 2008 R2

I want to delete the characters after numbers in one of my columns:

Column values are like this :

2GB 
3G
28GB
7G
90G

as you can see there is no pattern in these numbers except I have a one or two digits number and letter G or GB after them.

What query can detect the numbers and delete the characters after that?

Thanks

Try this..

drop table #t
create table #t(id varchar(10))
insert into #t values('2GB'), 
            ('3G'),
            ('28GB'),
            ('7G'),
            ('90G')


            update #t
            set id=substring(id,0,PATINDEX('%[GB]%',id)) from #t
            select * from #t

See Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM