Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 finding string

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2013-06-17 : 19:25:12
I need concantenate only those words until ":" from the starting point
(ie) output should be:
'New Member Territory "ADTest" :' and from the last only the rollup territory value
(ie) ' RollupTerritory "INT"'
so my final o/p should be: New Member Territory "ADTest" : RollupTerritory "INT"


declare @str nvarchar(max)
set @str='New Member Territory "ADTest" : Description "ANDORRA" : ParamountRegion "SOE" : Region "" : RegionDescription "SouthernEurope" : Country "A" : CountryDescription "Andor" : District "" : City "" : Area "" : TaxJurisdiction "" : Location "06" : RollupTerritory "INT"'
print @str

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-17 : 20:07:31
[CODE]


declare @str nvarchar(max)
set @str='New Member Territory "ADTest" : Description "ANDORRA" : ParamountRegion "SOE" : Region "" : RegionDescription "SouthernEurope" : Country "A" : CountryDescription "Andor" : District "" : City "" : Area "" : TaxJurisdiction "" : Location "06" : RollupTerritory "INT"'
print @str
SELECT SUBSTRING(@str, 1, CHARINDEX( ':', @Str, 1)) +
SUBSTRING(@str, (LEN(@Str) - CHARINDEX(':', REVERSE(@str), 1))+2, 1+CHARINDEX(':', REVERSE(@str), 1)) AS FINALSTRING;

[/CODE]
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-06-17 : 21:47:18
select left(@str, CHARINDEX( ':', @Str, 1)-1) +right(@str, CHARINDEX( ':', reverse(@Str), 1))
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-17 : 23:22:05
quote:
Originally posted by waterduck

select left(@str, CHARINDEX( ':', @Str, 1)-1) +right(@str, CHARINDEX( ':', reverse(@Str), 1))


Your query is much better. Thank you.
Go to Top of Page
   

- Advertisement -