Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

add values inside a * delimited string sql server 2008

Posted on 2013-01-08
2
Medium Priority
?
316 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 2000 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

722 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