Solved

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

Posted on 2013-06-18
2
297 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
MS SQL BCP Extra Lines Between Records 2 19
VB.NET 2008 - SQL Timeout 9 24
SQL Server Error 21 8 24
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

777 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