Solved

T-SQL: querying nvarchar column with pipe delimited data

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now