无需函数的 PL/SQL 基础转换
在 Oracle 10g 中,有没有办法将十进制转换为二进制,或二进制转换为十进制,而无需先定义函数?我的数据库访问权限有限(仅限 SELECT),并且我在网上找到的所有解决方案似乎都涉及 CREATE FUNCTION
,这对我不起作用。
Is there any way to convert decimal to binary, or binary to decimal, in Oracle 10g without having to first define a function? I have limited database access (SELECT only) and all the solutions for this I've found online seem to involve CREATE FUNCTION
, which does not work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果十六进制足够好,则 TO_CHAR 和 TO_NUMBER 可以工作:
您可以使用
RAWTOHEX()
和HEXTORAW()
函数将十六进制转换为二进制,如下所示出色地。If hexadecimal is good enough, then TO_CHAR and TO_NUMBER can work:
You may be able to use the
RAWTOHEX()
andHEXTORAW()
functions to make the hex to binary transition as well.Frank Zhou 专门研究棘手的 SQL 难题,为这个问题设计了一个纯 SQL 解决方案。您可以在 他的 OraQA 网站。但请注意:这确实很粗糙。
更新
OraQA 的原始链接已损坏:The Wayback Machine 在这里有一个存档版本。
Frank Zhou who specializes in gnarly SQL puzzlers has devised a pure SQL solution for this problem. You can find it on his OraQA site. But be warned: it is really gnarly.
update
Original link to OraQA is broken: The Wayback Machine has an archived version here.
您能否在 SQLPlus 脚本中执行 PL/SQL,如下所示:
我不确定,但我不认为该函数会在数据库中永久创建,我认为它只会在脚本运行期间暂时存在,所以这个 < em>可能有效。值得一试,对吧? ;)
或者,如果这不起作用,您可以 SELECT ... from Dual 进行转换,尽管这可能会很尴尬,并且只有在您知道位数的情况下才有效 - 也许(我'如果我能抽出几分钟时间并且可能的话,我会尝试将其组合在一起)。
Can you execute PL/SQL in an SQLPlus script like this:
I'm not sure but I don't think the function will be created permanently in the database, I think it will only exist temporarily for the duration of the script so this might work. It's worth a shot, right? ;)
Or if that doesn't work, you could
SELECT ... from dual
to convert, though that will probably be awkward and will only work if you know the number of digits - maybe (I'll try to throw it together if I can get a few minutes, and if it's possible).十进制到二进制的一个粗略但直接的解决方案:
二进制到十进制会比较棘手。您也许可以使用
connect by
将字符串拆分为 4 个字符的段,以类似的方式转换它们,然后将它们重新连接在一起(第二个connect by
使用SYS_CONNECT_BY_PATH
?),但这对我今晚来说有点太乏味了。再想一想,这是二进制到十进制的解决方案(我很喜欢
connect by
问题):此解决方案一次仅适用于当前编写的一行。要使其适用于多行,您需要向最外面的
connect by
添加某种唯一标识符。A crude, but straight-forward solution for decimal-to-binary:
Binary-to-decimal would be trickier. You might be able to use
connect by
to split the string into 4-character segments, convert them in a similar fashion, then concatenate them back together (a secondconnect by
usingSYS_CONNECT_BY_PATH
?), but that's a little too tedious for me to work out tonight.On second thought, here's the Binary-to-decimal solution (I'm a sucker for
connect by
problems):This solution only works for one row at a time as currently written. To make it work with multiple rows, you'd need to add some sort of unique identifier to the outermost
connect by
.我在 Oracle 中尝试使用 CONNECT BY 在简单的 SELECT 语句中将十进制转换为二进制。终于得到了想要的输出。您可以使用下面的,它工作正常。
I was trying this in Oracle using
CONNECT BY
to covert decimal to binary in a simpleSELECT
statement. Finally got the desired output. You can use the below, it is working fine.