Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-04-18 : 03:04:25
|
Sentence:[Mr/Mrs XuserX from XLocationX has created a new bill.<br><br><b>Bill Details</b><table border=0><tr><td>Amount:</td><td>XAmountX</td></tr><tr><td>Vendor Name:</td><td>XVendorX</td></tr><tr><td>Payment Due Date:</td><td>XDueDateX</td></tr><tr><td>Remarks:</td><td>XRemarksX</td></tr></table><table width="80%"><tr><td><p style="text-align:justify;">The information contained in this e-mail message and any files/attachment transmitted with itis confidential and for the sole use of the intended recipient(s) or entity identified. If you arenot the intended recipient, please contact us at Technology@impetusconsulting.net and destroy/delete allcopies and attachment thereto along with the original message.</p></td></tr></table>]From the above Sentence I need the Output as:XuserX,XLocationX,XAmountX,XVendorX,XDueDateX,XRemarksXWords which are Started with X and Ending with X were the values which i needed from the Sentence with Comma Separated. Please help me with this from Select SQL Query.Regards,Kalaiselvan RLove Yourself First.... |
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-04-18 : 05:13:30
|
Hi, If its confusing, am asking simply. Sentence:[Mr/Mrs XuserX from XLocationX has created new Bill. <br></b><table><tr><td>Amount:</td><td>XamountX</td></tr></table>Output Must Be:XuserX,XLocationX,XamountXLetter starts with X and Ends With X must repeated with commas from that sentence. Please am in urgent to finish this. Help me to Built a Query for this...Regards,Kalaiselvan RLove Yourself First.... |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-18 : 09:46:59
|
I suppose this is very similar to splitting a comma-separated list into a table. There are a number of ways to do this - which are all listed nicely here http://www.sommarskog.se/arrays-in-sql-2005.html with examples. You can pick any that you feel comfortable with. One of the approaches is using a recursive CTE - I like this because it does not leave any foot-prints, i.e., you don't need to create any UDFs. Also, it does not require a while loop. If you decide to go that route, here is a way to implement that.DECLARE @str VARCHAR(MAX);SET @str = '[Mr/Mrs XuserX from XLocationX has created a new bill.<br><br><b>Bill Details</b><table border=0><tr><td>Amount:</td><td>XAmountX</td></tr><tr><td>Vendor Name:</td><td>XVendorX</td></tr><tr><td>Payment Due Date:</td><td>XDueDateX</td></tr><tr><td>Remarks:</td><td>XRemarksX</td></tr></table><table width="80%"><tr><td><p style="text-align:justify;">The information contained in this e-mail message and any files/attachment transmitted with itis confidential and for the sole use of the intended recipient(s) or entity identified. If you arenot the intended recipient, please contact us at Technology@impetusconsulting.net and destroy/delete allcopies and attachment thereto along with the original message.</p></td></tr></table>]';;WITH cte(id , loc ) as( SELECT 1, CHARINDEX('X',@str,1) UNION ALL SELECT c.id+1, CHARINDEX('X',@str,c.loc+1) FROM cte c WHERE CHARINDEX('X',@str,c.loc+1) > 0)SELECT a.id,b.id, SUBSTRING(@str,a.loc,b.loc-a.loc+1)FROM cte a INNER JOIN cte b ON b.id = a.id+1WHERE a.id%2 = 1; |
 |
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-04-19 : 03:14:29
|
Hi, CTE is good and started learning that but The above CTE expression using While Loop gives the Output as Datatable. But i need it with comma separated. I have just said the sample for this, Actually Its a Column in my table. Ex: MY TABLE[ID] [FROM] [TO] [CONTENT] 1 abc@gmail.com def@gmail.com @str1 2 abc@yahoo.com def@yahoo.com @str2Need OutPut as:[ID] [FROM] [TO] [CONTENT] 1 abc@gmail.com def@gmail.com XVendorX,XDueDateX,XRemarksX 2 abc@yahoo.com def@yahoo.com XDueDateX,XRemarksXHere the Column content is our @str value the Sentence Data. So i ned it in single table and content must be returned with comma seperated values which starts and Ends by X.Regards,Kalaiselvan RLove Yourself First.... |
 |
|
matty
Posting Yak Master
161 Posts |
Posted - 2011-04-19 : 05:22:09
|
I have made changes in Sunita's query to use table.;WITH cte(Id,RowId , loc ) as( SELECT Id,1, CHARINDEX('X',Content,1) FROM MyTable UNION ALL SELECT t.Id,c.RowId+1, CHARINDEX('X',Content,c.loc+1) FROM cte c join MyTable t ON c.Id = t.Id WHERE CHARINDEX('X',Content,c.loc+1) > 0)SELECT Id, STUFF(( SELECT ', ' + SUBSTRING(Content,a.loc,b.loc-a.loc+1) FROM cte a INNER JOIN cte b ON b.RowId = a.RowId+1 INNER JOIN MyTable t ON t.Id = t1.Id AND a.Id = t.Id AND b.Id = t.Id WHERE a.RowId%2 = 1 FOR XML PATH ('')) ,1,1,'' ) AS ContentFROM MyTable t1 |
 |
|
|
|
|
|
|