?
Solved

SQL - Extract string between two identical characters

Posted on 2011-05-10
4
Medium Priority
?
9,450 Views
Last Modified: 2012-08-14
Experts,

This may be a relatively simple question, but I've just spen the last few hours trying to implement different solutions to make it work, all with the same results.

I have a SQL table that contains the organization's computers.  All of the PC names are identical in their format, and for that field there are a ton of records that have this as their data:

SITE - ROOM# - SERIAL#      So, an actual PC name could be "SITE-123-123A".

I need to extract the room numbers only.  The value between the two hyphens.  I've got the code (in the code block below), that ~HALFWAY~ works, but can't seem to masage the rest of this.

VB is a much better way for me to do this, but I'm limited to forcing this into SQL.  Any assistance you guys can provide would be greatly appreciated.

SELECT SUBSTRING(computer_name, CHARINDEX('-', computer_name) + 1, LEN(computer_name)) AS Test
FROM ComputerNameTable

Open in new window

0
Comment
Question by:usslindstrom
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35735127
SELECT SUBSTRING(computer_name, CHARINDEX('-', computer_name) + 1, LEN(computer_name) - CHARINDEX('-', REVERSE(computer_name))) AS Test
FROM ComputerNameTable
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35735153
You need to SUBSTRING your resultant string again just grabbing this first part:
 
SELECT SUBSTRING( SUBSTRING(computer_name,
                  CHARINDEX('-', computer_name) + 1,
                  LEN(computer_name)),
       0,
       CHARINDEX('-', computer_name) - 1) AS Test
FROM ComputerNameTable

Open in new window

0
 
LVL 3

Accepted Solution

by:
Krtyknm earned 2000 total points
ID: 35735161
I believe the below code will work.
SELECT 
	REPLACE 
	(
		LEFT
		(
			SUBSTRING
			(
				computer_name,
				CHARINDEX
				(
					'-',computer_name
				)+1,
				LEN
				(
					computer_name
				)
			),
			CHARINDEX
			(
				'-',
				SUBSTRING
				(
					computer_name,
					CHARINDEX
					(
						'-',
						computer_name
					)+1,
					LEN
					(
						computer_name
					)
				)
			)
		)
		,'-',
		''
	)
FROM ComputerNameTable

Open in new window

1
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 35735188
Holy Crap you guys are quick!

Thank you everyone for the help.  Unforuntalely, I got some small errors when I tried running the first two examples.

Krtyknm: - your solution worked like a champ.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question