Search code examples
sqlsql-serversql-server-2008-r2charindex

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


Solution

  • 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