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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select the middle characters of the column value

Author  Topic 

abul_mohsin
Starting Member

21 Posts

Posted - 2013-12-19 : 04:07:28
Dears
Need help for a select statement.

column data : "CN=Doctor John Tom,OU=Migrated Users,DC=ABC,DC=COM"

Expected value of CN : "Doctor John Tom"

Thanks in advance.

Thanks & Best Regard's
Abul Mohsin

Kristen
Test

22859 Posts

Posted - 2013-12-19 : 04:24:43
[code]
SELECT SUBSTRING(MyColumn,
CHARINDEX('CN=', MyColumn)+3,
CHARINDEX(',', SUBSTRING(MyColumn+',', CHARINDEX('CN=', MyColumn), LEN(MyColumn)+1))-1-3
)
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-19 : 04:33:57
Is the format consistent always?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

abul_mohsin
Starting Member

21 Posts

Posted - 2013-12-19 : 04:47:08
Yes its always same but with different value of CN.

quote:
Originally posted by visakh16

Is the format consistent always?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Thanks & Best Regard's
Abul Mohsin
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2013-12-19 : 05:31:59
quote:
Originally posted by abul_mohsin

Yes its always same but with different value of CN.


If CN is guaranteed to ALWAYS be the first entry, and there will ALWAYS be at least a second entry after it, then you could use a more simple method.

My code, above, will work if CN is in first, middle, or last position
Go to Top of Page
   

- Advertisement -