Solved

add values inside a * delimited string sql server 2008

Posted on 2013-01-08
2
309 Views
Last Modified: 2013-01-08
hi,
I've a situation here, i need to add the values inside a table field. the values in that field is * delimited for example 160.00*120.00*****************************. my sql is very simple and straightforward as below.

SELECT Ref, (left(DateOfServiceFrom,10)+ '-' +right(REPLACE(DateOfServiceTo, '*', ''),10)) AS Episodes,  ServiceCharges, DateClaimSent
    FROM Claim

Open in new window


ServiceCharges = 160.00*120.00*****************************

so when i run my query, ServiceCharges value need to come 280.00. In some cases the values are also vary like 160.00*120.00**********20.00******************
so in that case, the ServiceCharges value should be 300.00.
i wrote the select statement
select replace (rtrim(replace('160.00*120.00*****************************','*',' ')),' ','+') AS PLUSDELIMVALUES

Open in new window

but the result come as 160.00+120.00
any help, comment or feedback would be deeply appreciated.
thanks,
0
Comment
Question by:s_hausen
[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
  • 2
2 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38757546
try this using XML data type.

This code returns 280.00
Select 
sum(n.r.value('.', 'decimal(12,2)'))
from (
select cast('<r>'+ replace('160.00*120.00*****************************','*','</r><r>') + '</r>' as xml)) as
S(XMLCol)
cross apply S.XMLCol.nodes('r') as n(r)
where n.r.value('.', 'varchar(10)') <> ''

Open in new window


try applying same logic in your query,
SELECT Ref, (left(DateOfServiceFrom,10)+ '-' +right(REPLACE(DateOfServiceTo, '*', ''),10)) AS Episodes,  
 DateClaimSent, sum(n.r.value('.', 'decimal(12,2)'))
    FROM Claim 
cross apply (
select cast('<r>'+ replace(ServiceCharges,'*','</r><r>') + '</r>' as xml)) as S(XMLCol)
cross apply S.XMLCol.nodes('r') as n(r)
where n.r.value('.', 'varchar(1)') <> ''
Group by Ref, (left(DateOfServiceFrom,10)+ '-' +right(REPLACE(DateOfServiceTo, '*', ''),10)) ,
DateClaimSent

Open in new window

0
 
LVL 39

Expert Comment

by:appari
ID: 38757547
should mention i got the idea of using XML from another question:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_27989103.html#a38757500
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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