Solved

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

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

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:
ScottPletcher 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query to display duplicates ? 6 27
Row-Level Security 2 20
SQL JOIN + SUBQUERY? 3 14
Update in Sql 7 0
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now