Question

I have a flat file with below list of amounts, could you please tell me , how can I make this below list of amount into two point decimal value something like 1234567.80 which are ending with {,A,H,E,C,I,F by using SQL?

12345678{
00484326A
00000210H
00000185A
00000077E
00000833C
00000255I
00000077E
00000039F
00000088A
00000000F
00000000A
00000100{  

Thank You,

Was it helpful?

Solution

Try this as with this SQLfiddle. Not pretty but it works

SELECT 
    CAST(
        CONCAT(SUBSTRING(test_value,1, LENGTH(test_value) -2),
               '.',
               SUBSTRING(test_value, LENGTH(test_value) -1, 1))
    AS DECIMAL(7,1))
FROM TEST
WHERE SUBSTRING(test_value, LENGTH(test_value)) = 'A' 
|| SUBSTRING(test_value, LENGTH(test_value)) = 'H'
-- keep adding above line for the rest of the ending characters you want

OTHER TIPS

I wrote this as a function because I think it's easier to read than inline code:

create function dbo.convert_amount_str ( @amount_str varchar(50) )
returns money
as
begin
declare @char_index int
declare @amount money
declare @char varchar(50)
declare @decimal money

-- Match the first non-numeric character
select @char_index = PATINDEX('%[^0-9]%', @amount_str)

-- Get the numeric characters into a numeric variable
select @amount = convert(money, SUBSTRING(@amount_str, 0, @char_index))

-- Get the non-numeric character (will work for multiple characters)
select @char = SUBSTRING(@amount_str, @char_index, (len(@amount_str) - @char_index) + 1)

-- Convert the non-numeric characters into decimal amounts
select @decimal = case @char
    when 'A' then .8 -- whatever this should equate to
    when 'H' then .7 -- whatever this should equate to
    -- output for remaining characters
    end

return @amount + @decimal
end

Then just use it like this:

select dbo.convert_amount_str('00484326A')

Or, more likely, referencing whatever column contains the numeric string values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top