Solved

T-SQL: querying nvarchar column with pipe delimited data

Posted on 2013-11-19
4
1,585 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

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Trigger 8 35
SQL server client app 3 26
Regarding Disk IO 3 42
SSRS Page Header from Group Data 2 23
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

761 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