Solved

SQL question

Posted on 2012-03-30
9
258 Views
Last Modified: 2012-04-06
I need a view that will parse a text column populate like this:

aaaaaaaa Annual: bbbbbbbb   Annual: cccccccc Annual: dddddddd   etc.

I need it to parse based on the token "Annual:" and then return 1 row per parsed item, like:

bbbbbbbb
cccccccc
dddddddd
0
Comment
Question by:HLRosenberger
9 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 250 total points
ID: 37789881
I don't believe that would be possible with a view - you can do it with a stored procedure/cursor like so....

CREATE PROC parseAnnualValues
	AS
	BEGIN
	
		SET NOCOUNT ON
		
		DECLARE @valString varchar(200)
		DECLARE @xmlValues AS XML	
		
		--create a temp table to hold the values until after all rows are processed
		CREATE TABLE #tmpTable (txtVal varchar(50))
		
		DECLARE myCursor CURSOR FOR
		SELECT myUnparsedTextColumn FROM myTable		<--change this 
		
		OPEN myCursor
		
		FETCH NEXT FROM myCursor 
		INTO @valString
		
		WHILE @@FETCH_STATUS = 0
		BEGIN
						
			--parse the list as xml		
			SET @xmlValues = CAST(('<val>' + replace(@valString, 'Annual: ', '</val><val>') + '</val>') AS XML)
				
			--insert the xml values into the temp table
			INSERT INTO #tmpTable
			SELECT N.value('.', 'varchar(50)') FROM @xmlValues.nodes('val') AS T(N)
			
			FETCH NEXT FROM myCursor 
			INTO @valString
		END
		
		CLOSE myCursor
		DEALLOCATE myCursor
		
		SELECT txtVal AS 'Annual' FROM #tmpTable
		DROP TABLE #tmpTable
	END

Open in new window

0
 
LVL 9

Assisted Solution

by:OCDan
OCDan earned 250 total points
ID: 37790791
This might work for you create the below function:
CREATE FUNCTION [dbo].[Split]
(    
    @RowData NVARCHAR(MAX),
    @Delimeter NVARCHAR(MAX)
)
RETURNS @RtnValue TABLE
(
    ID INT IDENTITY(1,1),
    Data NVARCHAR(MAX)
)
AS
BEGIN
    DECLARE @Iterator INT
    SET @Iterator = 1

    DECLARE @FoundIndex INT
    SET @FoundIndex = CHARINDEX(@Delimeter,@RowData)

    WHILE (@FoundIndex>0)
    BEGIN
        INSERT INTO @RtnValue (data)
        SELECT
            Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))

        SET @RowData = SUBSTRING(@RowData,
                @FoundIndex + DATALENGTH(@Delimeter) / 2,
                LEN(@RowData))

        SET @Iterator = @Iterator + 1
        SET @FoundIndex = CHARINDEX(@Delimeter, @RowData)
    END
   
    INSERT INTO @RtnValue (Data)
    SELECT Data = LTRIM(RTRIM(@RowData))

    RETURN
END

Then invoke it using below as a query (don't think this could be done in a view)

declare @row as nvarchar(max)
set @row = 'Annual: bbbbbbbb   Annual: cccccccc Annual: dddddddd'
select * from [dbo].[Split] (@row, 'Annual: ')
where len(data)>0

with the @row being replaced with your string.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37810541
Thanks
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37812037
I meant to give credit to both answers. How do I change this?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 34

Expert Comment

by:James0628
ID: 37815063
You can use the "Request Attention" link below your original post and ask to have the question re-opened.  And you can probably delete the new question that you posted about this and continue things here.

 FWIW, I can't help with the error that you mentioned in the new question.  I've never used XML data and don't know why you're getting that error.

 James
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37815806
micropc1:   Your answer about a stored proc.  This line gives me an error:

SELECT N.value('.', 'varchar(50)') FROM @xmlValues.nodes('val') AS T(N)

The error is with N.value.   Could you please help me with why this is an error?
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 37816492
Thanks
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37816586
Were you able to figure out the error? I haven't had a chance to look at it, but my guess would be that it isn't able to format the string correctly as xml. Do you have any nulls in your data? You may need to filter those out.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

17 Experts available now in Live!

Get 1:1 Help Now