Solved

add values inside a * delimited string sql server 2008

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 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

13 Experts available now in Live!

Get 1:1 Help Now