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
How to remove text followed by a special characted in MSQL
-
21-07-2023 - |
Pergunta
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
Solução
Outras dicas
Why LEN(names)-CHARINDEX('/',names)?
try using CHARINDEX('/',names)+1 instead.
As far as I remember, SUBSTRING's third variables should be the length of the desired output string.
A more complex query to help you out:
DECLARE @Text VARCHAR(50) = 'IRELAND/IRLANDE GREECE/GRÈCE DENMARK/DANEMARK'
SELECT
SUBSTRING(SUBSTRING(@Text,0,CHARINDEX(' ',@Text,0)),0,CHARINDEX('/',@Text,0))
+
SUBSTRING(SUBSTRING(@Text,CHARINDEX(' ',@Text,0),CHARINDEX(' ',@Text,0)),0,CHARINDEX('/',@Text,0))
+ ' '
+
REPLACE(REVERSE(SUBSTRING(REVERSE(@Text),CHARINDEX('/',@Text,0) + 1 ,CHARINDEX('/',@Text,0))),'/', ' ')
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow