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
Solved

I need help building a query that will parse data into a temp table

Posted on 2013-06-18
2
299 Views
Last Modified: 2013-06-20
Hi Experts,
I have an integrated service that imports raw data from a text file into a SQL Server table.  I want to build a temp table using a SELECT query that will give me a column with transaction amounts, by parsing the raw data starting at position 20 for a length of 13 characters.  Once parsed, the data looks like the samples below.  

I need to remove all leading 0's and then sum the amounts, and make the 2 right most characters decimals. How can I do this?  The raw data is originally imported to a varchar(250) column.


Sample of raw data:

0000000012500
00000000-1500
0000005200025
00000000-4500

The sum should be:  $52,065.25



Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
2 Comments
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 39256884
Many ways to do that, e.g.:
select sum(convert(money, substring(rawdata, patindex('%[-1-9]%', rawdata),100))/100) from 
(select rawdata = '0000000012500'
union all
select '00000000-1500'
) data

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39260101
I would convert to decimal only at the end, to avoid any rounding issues:


SELECT CAST( SUM( CAST(SUBSTRING(raw_data, PATINDEX('%[^0]%', raw_data), 100) AS bigint) ) / 100.0 AS decimal(19, 2))
FROM (
    SELECT '0000000012500' AS raw_data UNION ALL
    SELECT '00000000-1500' UNION ALL
    SELECT '0000005200025' UNION ALL
    SELECT '00000000-4500'
) AS test_data
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

Suggested Solutions

Title # Comments Views Activity
SQL: Get Holding Day Based On Transfer Date And Return Date 13 74
Trouble with <> 2 21
SQL server 2014 replication error 20 38
SQL - Simple Pivot query 8 15
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

838 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