?
Solved

SQL question

Posted on 2012-03-30
9
Medium Priority
?
270 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
[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
9 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 1000 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 1000 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 37812037
I meant to give credit to both answers. How do I change this?
0
 
LVL 35

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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