Solved

T-SQL: querying nvarchar column with pipe delimited data

Posted on 2013-11-19
4
1,613 Views
Last Modified: 2013-12-06
hi, we have nvarchar column containing data like this:

aaaa|bbbb|cccc|dddd|.....

What is the best way to get 'cccc' value (third value) ?

Thanks.
0
Comment
Question by:quasar_ee
[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
4 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39661580
DECLARE @CSV_String varchar(50) = 'aaaa|bbbb|cccc|dddd';
select dbo.fn_GetNthValue(@CSV_String, '|', 3);

Open in new window

Function DROP AND CREATE:
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetNthValue]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GetNthValue]
GO

CREATE FUNCTION [dbo].[fn_GetNthValue]
  (
   @string VARCHAR(MAX)
 , @delimiter CHAR(1)
 , @Position int
  )
RETURNS VARCHAR(max)
  BEGIN
	DECLARE @OutputValue varchar(max);
    DECLARE
      @start INT
    , @end INT
    SELECT
      @start = 1
    , @end = CHARINDEX(@delimiter, @string)
    DECLARE @Counter int = 0;
    WHILE @start < LEN(@string) + 1 
      BEGIN
		SET @Counter = @Counter+1;
        IF @end = 0 
          SET @end = LEN(@string) + 1

		IF @Counter = @Position
			SET @OutputValue = SUBSTRING(@string, @start, @end - @start)
			
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
      END
    RETURN @OutputValue
  END
GO

Open in new window

0
 
LVL 25

Expert Comment

by:chaau
ID: 39661590
If it is a always a third element you are after you can use CHARINDEX a couple of times, like this:

DECLARE @test varchar(100)
SET @test = 'aaaa|bbbb|cccc|dddd|'
SELECT CASE WHEN CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) > 0
 THEN 
   CASE WHEN CHARINDEX('|', @test, CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) + 1) > 0
   THEN
     SUBSTRING(@test, CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) + 1, CHARINDEX('|', @test, CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) + 1) - CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) - 1)
   ELSE
     SUBSTRING(@test, CHARINDEX('|', @test, CHARINDEX('|', @test) + 1) + 1, 1000)
   END
 END

Open in new window

0
 
LVL 11

Expert Comment

by:Louis01
ID: 39661594
p.s. In my function above, line 27 & 28 should really be:
IF @Counter = @Position
BEGIN
    SET @OutputValue = SUBSTRING(@string, @start, @end - @start)
    BREAK
END
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39661611
>>What is the best way to get 'cccc' value
to store it as a field in a normalized record structure (not as part of a string)

but I realize that's not the answer you are seeking
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

751 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