Solved

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

Posted on 2013-06-18
2
302 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
[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 Comments
 
LVL 70

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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