sql server - How to extract strings between two special different characters in TSQL -


i using code following question: how extract strings between 2 special characters in tsql

declare @c varchar(100) set     @c = 'microsoft.systemcenter.useractionmanager:servername_1.domain.net;servername_2.domain.net'   select substring(@c, charindex(':', @c) + 1, len(@c) - charindex('.', @c) - charindex(':', @c)) 

current result:

servername_1.domain.net;servername_2. 

i want search column/string , extract string starting @ ':' , going first '.'.

the result trying is: servername_1

the caveat server name vary in length.


also, when try run following query:

select  substring(column_name, charindex(':', column_name) + 1, len(column_name) - charindex(':', column_name) - charindex(':', column_name)) [dbo].[servernametable]  

i following error:

msg 537, level 16, state 3, line 1 invalid length parameter passed left or substring function. 

many in advance.

sql fiddle

this replace , including first : using stuff. uses same result find first . , substring stuff result first .

declare @c varchar(100) set     @c = 'microsoft.systemcenter.useractionmanager:servername_1.domain.net;servername_2.domain.net'  select substring(stuff(@c, 1, charindex(':',@c), ''), 0, charindex('.', stuff(@c, 1, charindex(':',@c), ''))) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -