2

I am doing a long to string conversion using java in following way.

Long longValue = 367L;
String str = Long.toString(longValue, 36).toUpperCase();

this is returning me as value A7. how can achieve this in doing oracle sql.

UPDATED:

Hi, I have analyzed how java code is working then wanted to implement the same thing in procedure.

First point is Input vaues. LONG and Radix. in my case Radix is 36. so i will have values from 1..9A...Z0 It picks up the values from this set only. Second point Long value as input. we have to divide this value with radix. if the quotient is more than 36 again we need to divide.

For eaxmple 367 then my converted value is 10(quotient) 7(remainder) that is A7. 3672 converted value is 102 0 i need to do again for 102 that is 2 -6 so my final value will be 2-6 0 that is 2U0(- means reverse the order).

UPDATE 2:

Using oracle built in functions we can do this. this was solved by my friend and gave me a function.I want to thank my friend. this will give me an out put as follows.

367 then my converted value is 10(quotient) 7(remainder) that is *A*7.(I modified this to my requirement).

FUNCTION ENCODE_STRING(BASE_STRING    IN    VARCHAR2,
                            FROM_BASE      IN    NUMBER,
                            TO_BASE        IN    NUMBER) 
  RETURN VARCHAR2
  IS
      V_ENCODED_STRING    VARCHAR(100);
  BEGIN

      WITH N1 AS (
                   SELECT SUM((CASE 
                                   WHEN C BETWEEN '0' AND '9' 
                                       THEN TO_NUMBER(C) 
                                   ELSE 
                                       ASCII(C) - ASCII('A') + 10 
                               END) * POWER(FROM_BASE, LEN - RN)    
                              ) AS THE_NUM
                   FROM (SELECT SUBSTR(BASE_STRING, ROWNUM, 1) C, LENGTH(BASE_STRING) LEN, ROWNUM RN 
                         FROM DUAL 
                         CONNECT BY ROWNUM <= LENGTH(BASE_STRING))
                  ),
           N2 AS ( 
                   SELECT (CASE 
                               WHEN N < 10 
                                    THEN TO_CHAR(N) 
                               ELSE CHR(ASCII('A') + N - 10) 
                           END) AS DIGI, RN
                   FROM (SELECT MOD(TRUNC(THE_NUM/POWER(TO_BASE, ROWNUM - 1)), TO_BASE) N, ROWNUM RN
                         FROM N1
                         CONNECT BY ROWNUM <= TRUNC(LOG(TO_BASE, THE_NUM)) + 1)
                  )
      SELECT SYS_CONNECT_BY_PATH(DIGI, '*') INTO V_ENCODED_STRING  
      FROM N2
      WHERE RN = 1    
      START WITH RN = (SELECT MAX(RN) FROM N2)
      CONNECT BY RN = PRIOR RN - 1;

      RETURN V_ENCODED_STRING;
4

2 回答 2

0

在 PL/SQL(或 Oracle SQL)中,您有一个名为 TO_CHAR 的函数。

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions181.htm

于 2013-06-20T06:20:20.847 回答
-1

在纯 SQL 中是不可能做到的。您必须使用 PL/SQL。

简单示例如何执行 PL/SQL:

CREATE TABLE long_tbl
(
    long_col LONG
);

INSERT INTO long_tbl VALUES('How to convert the Long value to String using sql');

DECLARE
    l_varchar VARCHAR2(32767);
BEGIN
    SELECT  long_col
    INTO    l_varchar
    FROM    long_tbl;

    DBMS_OUTPUT.PUT_LINE(l_varchar);
END;
-- How to convert the Long value to String using sql

有 TO_LOB 功能,但只能在将数据插入表时使用。 http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions185.htm

您只能将此函数应用于 LONG 或 LONG RAW 列,并且只能应用于 INSERT 语句中子查询的选择列表中。

通过使用“dbms_sql.column_value_long”还有其他更合适的方法,但这会变得复杂(获取 LONG 列并附加到 CLOB 类型。)

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sql.htm#i1025399

(Oracle 数据库 PL/SQL 包和类型参考)

于 2013-06-20T08:15:03.080 回答