Search code examples
sqlsubstringspecial-characterstrimcharindex

How to remove text followed by a special characted in MSQL


I have a table named 'country' with a column named 'name' and the names in this column appears with translation followed by special character '/'.

IRELAND/IRLANDE
GREECE/GRÈCE
DENMARK/DANEMARK

Now i want only the countrynames before this special character'/' so the out put should look like this..

IRELAND
GREECE
DENMARK

please help. Thanks in advance


Solution

  •  create table #t(name nvarchar(40))
    insert into #t values('IRELAND/IRLANDE')
    ,('GREECE/GRÈCE')
    ,('DENMARK/DANEMARK')
    
    
    select substring(name,0,CHARINDEX('/',name)) from #t