按字母顺序对varchar2中的字符进行排序 [英] Sort chars in varchar2 alphabetically

查看:53
本文介绍了按字母顺序对varchar2中的字符进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个函数,该函数可以按字母顺序对varchar2中的char进行排序.

I'm looking for a function that would sort chars in varchar2 alphabetically.

是否可以在oracle中内置某些东西,或者需要在PL/SQL中创建自定义?

Is there something built-in into oracle that I can use or I need to create custom in PL/SQL ?

推荐答案

来自 http://forums.oracle.com/forums/thread.jspa?messageID=1791550 这可能有效,但没有10g的测试...

From an answer at http://forums.oracle.com/forums/thread.jspa?messageID=1791550 this might work, but don't have 10g to test on...

SELECT MIN(permutations)
FROM (SELECT REPLACE (SYS_CONNECT_BY_PATH (n, ','), ',') permutations
    FROM (SELECT LEVEL l, SUBSTR ('&col', LEVEL, 1) n
        FROM DUAL
        CONNECT BY LEVEL <= LENGTH ('&col')) yourtable
    CONNECT BY NOCYCLE l != PRIOR l)
WHERE LENGTH (permutations) = LENGTH ('&col')

在示例中,col是在SQL * Plus中定义的,但是如果您将此功能设为函数,则可以传递它,或者可以重新构造它以直接获取表列.

In the example col is defined in SQL*Plus, but if you make this a function you can pass it in, or could rework it to take a table column directly I suppose.

我将其作为起点而不是解决方案;最初的问题是关于字谜的,所以它旨在查找所有排列,因此可能会进行相似但简化的操作.我怀疑这对于较大的值来说缩放效果不是很好.

I'd take that as a start point rather than a solution; the original question was about anagrams so it's designed to find all permutations, so something similar but simplified might be possible. I suspect this doesn't scale very well for large values.

这篇关于按字母顺序对varchar2中的字符进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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