仅使用SQL将Base 36转换为Base 10 [英] Base 36 to Base 10 conversion using SQL only

查看:100
本文介绍了仅使用SQL将Base 36转换为Base 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现了一种情况,我需要在SQL语句的上下文中执行从36到10的基数转换. Oracle 9或Oracle 10中似乎没有内置任何东西可以解决此类问题.我的Google-Fu和AskTom建议创建一个pl/sql函数来处理任务.目前,这对我来说不是一个选择. 我正在寻找可以帮助我解决此问题的方法的建议.

A situation has arisen where I need to perform a base 36 to base 10 conversion, in the context of a SQL statement. There doesn't appear to be anything built into Oracle 9, or Oracle 10 to address this sort of thing. My Google-Fu, and AskTom suggest creating a pl/sql function to deal with the task. That is not an option for me at this point. I am looking for suggestions on an approach to take that might help me solve this issue.

将其转换为可视形式...

To put this into a visual form...

WITH
Base36Values AS
(
    SELECT '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' myBase36 FROM DUAL
),
TestValues AS
(
    SELECT '01Z' BASE36_VALUE,
            71   BASE10_VALUE FROM DUAL
)
SELECT *
FROM Base36Values,
     TestValues

我正在寻找一些根据输入01Z计算值71的方法. 编辑-向后...给定01Z将其转换为71.

I am looking for something to calculate the value 71, based on the input 01Z. EDIT - that is backwards... given 01Z translate it to 71.

作为贿赂,每个有用的答案都会得到免费的投票.

As a bribe, each useful answer gets a free upvote.

谢谢

邪恶.

推荐答案

select sum(position_value) from
(
  select power(36,position-1) * case when digit between '0' and '9' 
                                     then to_number(digit)
                                     else 10 + ascii(digit) - ascii('A')
                                end
          as position_value
    from (
          select substr(input_string,length(input_string)+1-level,1) digit, 
                 level position
            from (select '01Z' input_string from dual)
            connect by level <= length(input_string)
         )
)

这篇关于仅使用SQL将Base 36转换为Base 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆