Solved

add values inside a * delimited string sql server 2008

Posted on 2013-01-08
2
265 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server memory Issue 7 74
Database connection opened on a machine 8 35
Azure SQL DB? 3 19
sql query Help 12 33
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now