Solved

SQL question

Posted on 2012-03-30
9
254 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Temporarily disable SQL Replication 7 23
Insert with SET how to handle join 6 33
SQL Maintenance Plan 3 17
Stored Procedure 2 0
'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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

8 Experts available now in Live!

Get 1:1 Help Now