Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Return first four values from delimited string

Posted on 2016-07-22
2
Medium Priority
?
35 Views
Last Modified: 2016-07-22
I'm a bit stuck
trying to return the first four values of string of varying length
Substrings is not consistent and parsename has a 4 limitiation

Sample
US-HVD-PROD-PNO-01-VM
EMEA-HVD-PROD-WGC-01-VM
APAC-HVD-UAT-PNO-02-VM

Need to return

US-HVD-PROD-PNO
EMEA-HVD-PROD-WGC
APAC-HVD-UAT-PNO

Any help much appreciated
0
Comment
Question by:Mauro Cazabonnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 41724857
wouldn't it be something like this:
DECLARE @value VARCHAR(100) = 'US-HVD-PROD-PNO-01-VM' -- EMEA-HVD-PROD-WGC-01-VM    APAC-HVD-UAT-PNO-02-VM
DECLARE @count INT = 0
DECLARE @start INT = 0

WHILE @count < 4
BEGIN
	SELECT @start, @count
	SET @start = CHARINDEX('-', @value, @start+1)
	IF @start = 0 
		BREAK
	ELSE
		SET @count = @count + 1
END

SELECT @start, SUBSTRING(@value, 0, @start)

Open in new window


you could easily create a UDF from it.
0
 
LVL 4

Author Closing Comment

by:Mauro Cazabonnet
ID: 41724978
Awesome thx!!!!!!
Set up as a UDF....
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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