• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Convert Long raw fields to varchar2 in Oracle with plain SQL

May 25, 2016 By _tasos 7 Comments

Oracle has a LONG RAW datatype that is used primarily in its system tables. There are cases where these tables need to be read from an application. There is a catch though, the common methods used to cast a datatype to another cannot be used in a plain SQL statement, but the developer must use PL/SQL in order to make this casting.

There is an alternative where with a plain SQL query we can retrieve the text representation of a LONG RAW datatype without using PL/SQL.

SELECT '' || regexp_substr( 
    extractvalue( 
                  dbms_xmlgen.getxmltype('SELECT high_value 
                                          FROM all_tab_partitions 
                                          WHERE table_name=''' || 
                                                TABLE_NAME || ''' AND
                                                table_owner = ''' || 
                                                table_owner || ''' AND 
                                                partition_name = ''' || 
                                                partition_name || ''''
                                         ), 
                  '//text()'
                ), 
    '''.*?''') 
FROM all_tab_partitions 
WHERE TABLE_NAME = 'MY_TABLE_WITH_PARTITIONS'

Our test case was that in a database we are working on a project, there was a table and one of its fields has some partitions defined, thus only allowing for this field to accept specific values. We needed a query to get those values and present them to the user with a DropDownList. As you can see, the query has in its WHERE clause a filter with the table name.

With the above query, we where able to retrieve these values, we only needed to apply a DISTINCT filter for our DropDownList.

Filed Under: Databases Tagged With: Oracle, SQL

Reader Interactions

Comments

  1. Kapil says

    February 5, 2020 at 13:19

    Excellent. This query saved a lot of efforts.

    Reply
  2. Glen Di Persio says

    March 8, 2020 at 14:50

    Brilliant. I had to replace REGEXP_SUBSTR() with TRIM() to use this against (*)_TAB_COLS.DATA_DEFAULT.

    Reply
  3. Ulf says

    May 28, 2020 at 16:14

    Hi, maybe a stupid question, but I only get NULL value from the 2050 partitions that are in my table?
    ‘||REGEXP_SUBSTR(EXTRACTVALUE(DBMS_XMLGEN.GETXMLTYPE(‘SELECTHIGH_VALUEFROMALL_T
    ——————————————————————————–

    ”||REGEXP_SUBSTR(EXTRACTVALUE(DBMS_XMLGEN.GETXMLTYPE(‘SELECTHIGH_VALUEFROMALL_T
    ——————————————————————————–

    Reply
    • _tasos says

      May 30, 2020 at 23:43

      Hi Ulf,

      Have you tried Glen’s suggestion (see comment) and replace REGEXP_SUBSTR() with TRIM()?

      Reply
      • Ulf says

        June 1, 2020 at 09:41

        Thx, TRIM solved my problem thanks!

        Regards

        /Ulf

        Reply
  4. Anthony Esposito says

    August 6, 2020 at 23:54

    Impressive. But how do you use this with a simple SELECT statement used to create a view.

    CREATE OR REPLACE myempview 
    AS
    SELECT first_name,
                 last_name, -- long column needing to convert to varchar2
                 address
    FROM myemp;
    Reply
    • _tasos says

      August 8, 2020 at 13:12

      Hi Anthony,

      I would try to use TO_LOB and then do a CAST.

      For example:

      SELECT
          first_name,
          CAST(TO_LOB(last_name) AS VARCHAR2)
      FROM myemp
      Reply

Leave a Reply to _tasos Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in