Solved

SQL question

Posted on 2012-03-30
9
263 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 37812037
I meant to give credit to both answers. How do I change this?
0
 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
database level memory cache..? 8 33
Format Data Field - SQL 11 37
Syntax Issue with SSIS module 26 105
TSQL recursive CTE challenge... 8 29
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 article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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