Solved

SQL question

Posted on 2012-03-30
9
265 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding SQL Server Browser in after install is complete 8 50
SQL- GROUP BY 4 46
How do I partition this table on date? 5 47
Complex SQL Server WHERE CLause 9 35
'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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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