Using MAX on a character column

How do I convert a varchar2 into a number
so I can use the MAX funtion on this?
If I say :
Select MAX(TO_NUMBER(type_code))
from tableAgives an error.
But if I say :
Select MAX(type_code)
from tableAit gives correct output.How is this correct?
Isnt it better to convert type_code into a number and use the
MAX function?

The concept of MAX and MIN apply equally well to strings as to numbers. Look at the first page of the Oxford English Dictionary, and the first word listed (I left my copy at home so I don't know what that word is) , that is equivalent to:
SELECT MIN(word)
FROM english_languageNow, look at the last page, that is:
SELECT MAX(word)
FROM english_languageThis does fall down when your strings are actually representations of numbers (which yours are obviously not), because of the string comparision semantics:
SQL> SELECT *
  2  FROM (SELECT '1' string_num FROM dual UNION ALL
  3        SELECT '2' FROM dual UNION ALL
  4        SELECT '10' FROM DUAL)
  5  ORDER BY string_num;
ST
1
10
2HTH
John

Similar Messages

  • Bug? using MAX() function on char(1) column returns something larger

    details:
    -- we have a complex nested query, where we are essentially returning the max() value into a variable
    -- the max() function is being used on a char(1) column
    -- where MAX() is part of an inner select, we have started getting
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    errors.
    SELECT MAX(X) INTO var FROM ... works in 9i and 10g
    SELECT X INTO var FROM (SELECT MAX(X) X FROM ... worked in 9i, does not work in 10g!
    -- We never had problems with the code until upgrading to 10g release 2.
    -- the Solution is to cast the final value with TO_CHAR(). The cast MUST be done at the outer most point of the select:
    SELECT TO_CHAR(X) INTO var FROM (SELECT MAX(X) X FROM ... works
    SELECT X INTO var FROM (SELECT TO_CHAR(MAX(X)) X FROM ... causes an error!
    The following script demonstrates the issue, and includes the solution:
    * October 3, 2006
    * Possible SQL bug introduced with Oracle 10G
    * Natalie Gray DBA/Developer, Environment Canada
    * Description:
    * Have discovered a problem with using the MAX() function
    * on columns of type char(1)
    * only an issue when used in an inner select
    * solution (see test 4)
    CREATE TABLE SQL_BUG_TEST
    X NUMBER,
    Y NUMBER,
    Z CHAR(1)
    INSERT INTO SQL_BUG_TEST (X, Y, Z)
    VALUES (1,1,'A');
    INSERT INTO SQL_BUG_TEST (X, Y, Z)
    VALUES (1,1,'B');
    INSERT INTO SQL_BUG_TEST (X, Y, Z)
    VALUES (1,2,'C');
    INSERT INTO SQL_BUG_TEST (X, Y, Z)
    VALUES (1,2,'D');
    DECLARE
    TYPE REC IS RECORD (
          x SQL_BUG_TEST.X%TYPE,
          y SQL_BUG_TEST.Y%TYPE,
          z SQL_BUG_TEST.Z%TYPE
    v_rec REC;
    BEGIN
          -- DISPLAY THE TABLE DATA
          BEGIN
                 DBMS_OUTPUT.PUT_LINE('TABLE DATA:');
                DBMS_OUTPUT.PUT_LINE('');
                 DBMS_OUTPUT.PUT_LINE('SELECT * FROM SQL_BUG_TEST ORDER BY X,Y,Z;');
                 FOR crs IN (SELECT *
                                     FROM SQL_BUG_TEST
                               ORDER BY X,Y,Z) LOOP
                    DBMS_OUTPUT.PUT_LINE(':'||crs.X||':'||crs.Y||':'||crs.Z);
                END LOOP;
          EXCEPTION WHEN OTHERS THEN
                 DBMS_OUTPUT.PUT_LINE(SQLERRM);       
          END;
          -- TEST 1
          -- returning result from MAX into a variable when the MAX is in the outer most select
          -- does not cause an error
          BEGIN
                 DBMS_OUTPUT.PUT_LINE('*****************************************************');
                 DBMS_OUTPUT.PUT_LINE('TEST 1');
                DBMS_OUTPUT.PUT_LINE('');
                 DBMS_OUTPUT.PUT_LINE('SELECT X, Y, MAX(Z) Z');
                DBMS_OUTPUT.PUT_LINE('FROM SQL_BUG_TEST');
                DBMS_OUTPUT.PUT_LINE('GROUP BY X,Y');
                FOR crs IN (SELECT X, Y, MAX(Z) Z
                                     FROM SQL_BUG_TEST
                               GROUP BY X, Y
                               ORDER BY X,Y,Z) LOOP
                     DBMS_OUTPUT.PUT_LINE(':'||crs.X||':'||crs.Y||':'||crs.Z);
                END LOOP;
          EXCEPTION WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE(SQLERRM);
          END;
          -- TEST 2
          -- returning MAX() from an inner select to an outer select and then into a variable
          -- causes an error
          BEGIN
                 DBMS_OUTPUT.PUT_LINE('*****************************************************');
                 DBMS_OUTPUT.PUT_LINE('TEST 2');
                DBMS_OUTPUT.PUT_LINE('THIS DID NOT CAUSE AN ERROR WITH ORACLE 9i');
                DBMS_OUTPUT.PUT_LINE('');
                 DBMS_OUTPUT.PUT_LINE('SELECT * INTO v_rec');
                DBMS_OUTPUT.PUT_LINE('FROM');
                DBMS_OUTPUT.PUT_LINE('(SELECT X, Y, MAX(Z) Z');
                DBMS_OUTPUT.PUT_LINE('FROM SQL_BUG_TEST');
                DBMS_OUTPUT.PUT_LINE('GROUP BY X,Y)');
                DBMS_OUTPUT.PUT_LINE('WHERE Y = 1');
                SELECT * INTO v_rec
                FROM
                (SELECT X, Y, MAX(Z) Z
                 FROM SQL_BUG_TEST
                 GROUP BY X, Y)
                WHERE Y = 1;
                DBMS_OUTPUT.PUT_LINE(':'||v_rec.X||':'||v_rec.Y||':'||v_rec.Z);
          EXCEPTION WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE(SQLERRM);
          END;
          -- TEST 3
          -- casting the result from MAX to char before returning to the outer select
          -- still causes an error
          BEGIN
                 DBMS_OUTPUT.PUT_LINE('*****************************************************');
                 DBMS_OUTPUT.PUT_LINE('TEST 3');
                DBMS_OUTPUT.PUT_LINE('');
                 DBMS_OUTPUT.PUT_LINE('SELECT * INTO v_rec');
                DBMS_OUTPUT.PUT_LINE('FROM');
                DBMS_OUTPUT.PUT_LINE('(SELECT X, Y, to_char(MAX(Z)) Z');
                DBMS_OUTPUT.PUT_LINE('FROM SQL_BUG_TEST');
                DBMS_OUTPUT.PUT_LINE('GROUP BY X,Y)');
                DBMS_OUTPUT.PUT_LINE('WHERE Y = 1');
                SELECT * INTO v_rec
                FROM
                (SELECT X, Y, to_char(MAX(Z)) Z
                 FROM SQL_BUG_TEST
                 GROUP BY X, Y)
                WHERE Y = 1;
                DBMS_OUTPUT.PUT_LINE(':'||v_rec.X||':'||v_rec.Y||':'||v_rec.Z);
          EXCEPTION WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE(SQLERRM);
          END;
          -- TEST 4 - SOLUTION
          -- the return value of MAX must be cast with to_char at the point where it is assigned to
          -- variable (outer most select)
          BEGIN
                 DBMS_OUTPUT.PUT_LINE('*****************************************************');
                 DBMS_OUTPUT.PUT_LINE('TEST 4 SOLUTION');
                DBMS_OUTPUT.PUT_LINE('');
                 DBMS_OUTPUT.PUT_LINE('SELECT X, Y, TO_CHAR(Z) Z INTO v_rec');
                DBMS_OUTPUT.PUT_LINE('FROM');
                DBMS_OUTPUT.PUT_LINE('(SELECT X, Y, MAX(Z) Z');
                DBMS_OUTPUT.PUT_LINE('FROM SQL_BUG_TEST');
                DBMS_OUTPUT.PUT_LINE('GROUP BY X,Y)');
                DBMS_OUTPUT.PUT_LINE('WHERE Y = 1');
                SELECT X, Y, TO_CHAR(Z) Z INTO v_rec
                FROM
                (SELECT X, Y, MAX(Z) Z
                 FROM SQL_BUG_TEST
                 GROUP BY X, Y)
                WHERE Y = 1;
                DBMS_OUTPUT.PUT_LINE(':'||v_rec.X||':'||v_rec.Y||':'||v_rec.Z);
          EXCEPTION WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE(SQLERRM);
          END;
    END;

    I certainly looks like a bug, but you should raise an iTAR on Metalink since Oracle does not monitor this forum.
    I was able to replicate your results on my 10.2.0.1 database.
    There is an easier workaround than yours. Try
    ALTER TABLE sql_bug_test MODIFY (z VARCHAR2(1));That seems to eliminate the problem on my instance.
    John

  • Is this a bug in OWB 11.2 - importing table metadata for character columns

    The Oracle® Warehouse Builder Data Modeling, ETL, and Data Quality Guide provides an overview of the data types supported.
    http://docs.oracle.com/cd/E11882_01/owb.112/e10935/orcl_data_objx.htm
    It says that for VARCHAR2 data type it saws (http://docs.oracle.com/cd/E11882_01/owb.112/e10935/orcl_data_objx.htm#CHDFIADI )
    "Stores variable-length character data. How the data is represented internally depends on the database character set. The VARCHAR2 data type takes a required parameter that specifies a maximum size up to 4,000 characters"
    That means , I guess, it says that when I import a table, any columns of type VARCHAR2(10) in the database should have its length show as characters in OWB, so a column of type Varchar2(10) in the Oracle database, should be shown as Varchar2(10) when imported into OWB table metadata via the OWB import function.
    However, if I have a database that set-up as a single-byte and import a table using the OWB import function a column that has a size of e.g. 10 in the database, is imported as OWB table metadata and the size is 10. Correct, I am happy.
    However, if the database is modified to support multi-byte characters, ALTUF16 encoding with the semantics set to "CHAR", then when I import the same table into OWB, OWB reports the size as 40, I guess its 40 bytes as in 10 characters @ 4 bytes per character.
    Is this a bug in OWB, as the datatype in the Oracle DB is varchar2(10), should OWB after importing a table not also report the column as VARCHAR2(10) ? Currently, is shows the column as varchar2(40).

    I noticed that myself in our project.
    Our varchars2 are defined as VARCHAR2(xxx CHAR) - OWB puts the size*4
    In fact if you have special characters like umlauts (ü,ä,ö,...) it will use 4 bytes per character.
    You can try it yourself. Define a Varchar2(1 CHAR) and manually change the size of the Column in your mapping inside OWB (in filters, joins or your target table).
    Then shoot an umlaut through the mapping and will end up with a "too small" error.
    Dont mind the size*4 issue - we totally ignored it and run without error since 4 years now.

  • Using MAX in transformations / Start Routine

    Hi,
    I am trying to use MAX function in an ABAP routine (transformation / Start) and it shows following error -
    "Unknown column name "MAX(/BIC/ZQMCOUNT)" until runtime, you cannot specify a field list.". Here is part of my code -
    >>
    data: v_counter(10) type n.
    SELECT MAX(/BIC/ZQMCOUNT)
    INTO v_counter
    FROM /BIC/AZDPM_DS300.
    <<
    Any thoughts please.
    Regards
    Vikash

    Hi there,
    Sometimes you have to put some spaces between the parantehsis in the max, min or count, like this:
    data: v_counter(10) type n.
    SELECT MAX( /BIC/ZQMCOUNT )
    INTO v_counter
    FROM /BIC/AZDPM_DS300.
    Can you try that?
    Diogo.

  • How to use MAX function in SSAS MDX Query

    I want to run this Query with MAX Condition on LAST_DATA_UPDATE Column .

    Hi Ashishsingh,
    According to your description, you want to know how to use MAX function in SQL Server Analysis Services MDX Query, right? In this case, please refer to the link below which describe the syntax and sample of MDX function.
    http://technet.microsoft.com/en-us/library/ms145601.aspx
    http://www.mdxpert.com/Functions/MDXFunction.aspx?f=64
    Hope this helps.
    Regards,
    Charlie Liao
    TechNet Community Support

  • Using Max function in ODI 11g

    Hi all,
    How to get max value of the column from the source table if the column is not used on the target side. suppose there are two same records in the source, i want to get the maximum value of created date record form source but this created date column is not used on the target side. Can you please help me with this
    Thanks

    Though question is bit ambiguous .
    You can write query for that particular column. i.e
    ( Select max(creation_date) from XYZ where (KEY columns ) )
    If the requirement is to find the record with maximum creation date , you can provide a filter on the source table like
    Where PQR.CREATION_DATE = ( Select max(creation_date) from XYZ where (KEY columns ) )
    Keep the execution area as Source as the table exist in source.
    Hope it helps.

  • Get the max count of the column list item

    Hi,
    I have a list with the 'TO' column of type people. My task is to get persons name who is featured the max number of time in the 'TO' column.
    What is the best possible solution OOB. I dont want to go with custom solution.
    Thanks,
    Ankit

    Hello Ankit,
    I understand that you want to get total of "To" column. I would suggest you modify the current view of list and use "Group By" and Total section to get Max number of that column. Just try this and check whether it fulfils your need or
    not.
    Hemendra:Yesterday is just a memory,Tomorrow we may never see<br/> Please remember to mark the replies as answers if they help and unmark them if they provide no help

  • Max number of ALV columns

    Hello everyone,
    I'm trying to create an ALV grid with 740 columns. My fieldcatalog has these, but when the ALV is shown, there are only the first 90 columns. Where is the rest?
    Is there a restriction as to lenghth of an outtab line? How many columns are viewable max?
    Are the columns with ls_fieldcatalog-no_out = 'X' counted in the max no. of columns?
    Thanks for your help,
    Friederike

    hi,
    The advantage of ALV isn't to can listed list longer than 255 char, but to can manage several colunms and decide which ones have to be showed and which ones hided.
    Anyway you there's a limit of the numbers of colunm of ALV grid.
    If you easly need to display an ABAP list longer than 255 char you can use NEW-PAGE statament.
    regards,
    keerthi.

  • Distinct and max more than 2 column

    Hi All,
    Need help here. I have one table and need to display latest revision. But the problem is, i cannot use DISTINCT for column TITLE and MAX also failed when column is more than 2. I'm really stuck
    I try below SQL statement but failed:
    SELECT title, MAX(revision), coordinator
    FROM specification
    GROUP BY title, coordinator
    But if my table have only 2 column, it's working:
    SELECT title, MAX(revision)
    FROM specification
    GROUP BY title
    Table Name: SPECIFICATION
    TITLE          REVISION          COORDINATOR
    SPEC-01          A                          John
    SPEC-01          B                          Michelle
    SPEC-01          C                          Andrew
    SPEC-01          D                          John
    SPEC-02          A                          Jenny
    SPEC-02          B                          Robert
    The result should be like below:
    TITLE          REVISION          COORDINATOR
    SPEC-01          D                          John
    SPEC-02          B                          Robert
    Edited by: 872733 on Jul 18, 2011 1:14 AM

    SQL> select * from v$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0      Production
    TNS for Linux: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production
    SQL> with t
      2  as
      3  (
      4   select 'SPEC-01' title, 'A' revision, 'John' coordinator from dual union all
      5   select 'SPEC-01', 'B', 'Michelle' from dual union all
      6   select 'SPEC-01', 'C', 'Andrew' from dual union all
      7   select 'SPEC-01', 'D', 'John' from dual union all
      8   select 'SPEC-02', 'A', 'Jenny' from dual union all
      9   select 'SPEC-02', 'B', 'Robert' from dual
    10  )
    11  select title, max(revision) revision, max(coordinator) keep(dense_rank first order by revision
    desc) coordinator
    12  from t
    13  group by title
    14 
    SQL> /
    TITLE   R COORDINA
    SPEC-01 D John
    SPEC-02 B RobertCheck this link for more information.
    http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions058.htm#SQLRF00641
    http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions001.htm#i81407
    Regards
    Raj

  • 1000 separator for character columns

    Hi,
    I m casting a number column to Character column to enable GOURL on the column.
    Currenly i m using the below data format for the GOURL column:
    In the column properties -> data format--> Treat Text As "HTML" -> Custom Text Format "@[html]@H"
    1. How to enable the comma separator(Use 1000's separator) when its character?
    2. How to enable Currency symbol for the character column?
    Any workaround for the above requirement would be appreciated
    Thanks
    Kranthi

    hi Kranthi,
    . How to enable the comma separator(Use 1000's separator) when its character?I dont think its possible
    2. How to enable Currency symbol for the character column?
    CAST(Col_name AS CHAR) || '$'
    If its already char direclty use || '$'
    thanks,
    Saichand.v

  • Issues with using the output redirection character with newer NXOS versions?

    Has anyone seen any issues with using the output redirection character with newer NXOS versions?
    Am receiving "Error 0x40870004 while copying."
    Simply copying a file from bootflash to tftp is ok.
    This occurs for both 3CDaemon and Tftpd32 softwares.
    Have tried it on multiple switches - same issue.
    Any known bugs?
    thanks!
    The following is an example of bad (NXOS4.1.1b) and good (SANOS3.2.1a)
    MDS2# sho ver | inc system
      system:    version 4.1(1b)
      system image file is:    bootflash:///m9200-s2ek9-mz.4.1.1b.bin
      system compile time:     10/7/2008 13:00:00 [10/11/2008 09:52:55]
    MDS2# sh int br > tftp://10.73.54.194
    Trying to connect to tftp server......
    Connection to server Established. Copying Started.....
    TFTP put operation failed:Access violation
    Error 0x40870004 while copying tftp://10.73.54.194/
    MDS2# copy bootflash:cpu_logfile tftp://10.73.54.194
    Trying to connect to tftp server......
    Connection to server Established. Copying Started.....
    |
    TFTP put operation was successful
    MDS2#
    ck-ci9216-001# sho ver | inc system
      system:    version 3.2(1a)
      system image file is:    bootflash:/m9200-ek9-mz.3.2.1a.bin
      system compile time:     9/25/2007 18:00:00 [10/06/2007 06:46:51]
    ck-ci9216-001# sh int br > tftp://10.73.54.194
    Trying to connect to tftp server......
    |
    TFTP put operation was successful

    Please check with new version of TFTPD 32 server. The error may be due to older version of TFPT server, the new version available solved this error. Files are getting uploaded with no issues.
    1. Download tftpd32b.zip from:
    http://tftpd32.jounin.net/tftpd32_download.html
    2. Copy the tftpd32b.zip file into an empty directory and extract it.
    3. Copy the file you want to transver into the directory containing tftpd32.exe.
    4. Run tftpd32.exe from that directory. The "Base Directory" field should show the path to the directory containing the file you want to transfer.
    At this point, the tftpserver is ready to begin serving files. As devices request files, the main tftpd32 window will log the requests.
    Best Regards...

  • At max how many logical columns can be created in RPD

    Hi All,
    At max how many logical columns can be created in RPD. I have a requirement of creating 200 columns. Will there be any problem .
    Is there any predefind number of columns for RPD creation??
    Please help ..

    Hi Annapurna,
    There's no limit that I'm aware of or which is mentioned anywhere. Just as an example: I have a logical fact table with around 750 logical columns (>500 original measures & 250 derived measures). No issue whatsoever. Opening the presentation table through answers takes about 2-3 seconds (the NQSQL command has a lot to retrieve), but that's about it.
    Cheers,
    C.

  • How to get max value of a column in VO?

    Hi ,
    i need to find the max value of a column named insured value in VO.
    Please help!!

    Hi,
    If this value is cuming from the Query then u can easily go for MAX function. If its a user enterable value then u need to iterate through all the rows for that column to find the max of them.
    Let me know.
    Regards,
    Gyan

  • How can i use Itemsyms in a first column?

    I want to use use Itemsyms in my first column of mulitlist box. But it always exist with first content of ItemNames. I want to
    know whether I can put a Itemsyms in a single column and data in other columns. Should i create a null string array and set it as
    a first column?  And the Itemsyms only can be put in the first column?
    I feel confused now after try many times. 
    please give me help.
    attached my vi.
    By the way, When i try to set number of row and column of list box, for column, the set value is 5, but the value get from property is 4.
    I don't know the reason.
    thanks
    br.
    Attachments:
    subvi_ListCheck.vi ‏16 KB
    main.vi ‏11 KB

    mmm2006 wrote:
    Hi,
    Thanks.
    How about other my questions? Please give me your advice.
    Thanks.
    mmm2006 wrote:
    I want to use use Itemsyms in my first column of mulitlist box. But it always exist with first content of ItemNames. I want to
    know whether I can put a Itemsyms in a single column and data in other columns. Should i create a null string array and set it as
    a first column?  And the Itemsyms only can be put in the first column?
    I feel confused now after try many times. 
    please give me help.
    I do not understand this term "Itemsyms".
    If you create a "null string array", your first column will become empty. You want this way or you want to replace your data in the first column? Both are different...
    You can very weel put your "Itemsyms" in a single column & data in others.
    - Partha
    LabVIEW - Wires that catch bugs!

  • Using max function in PL/SQL

    VERY URGENT...
    Am new to oracle...
    I've written a package that display gif images in form of histogram/bar chart. using html,
    I need to use max function to display values proportionately.
    please help. i need to complete this assignment by 2/9/00 by 10.00 am at the latest. I've half written a function but I don't know if there's a simpler way fo doing this. html enabled

    First of all Thanks to all gentlemen who replied ..many thanks ...
    Tried the ROW_NUMBER() option but still it is taking time...have given output for the query and tkprof results as well. Even when it doesn't fetch any record ( this is a valid cased because the input header id doesn't have any workflow request submitted & hence no entry in the wf_items table)..then also see the time it has taken.
    Looked at the RANK & DENSE_RANK options which were suggested..but it is still taking time..
    Any further suggestions or ideas as to how this could be resolved..
    SELECT 'Y', 'Y', ITEM_KEY
    FROM
    ( SELECT ITEM_KEY, ROW_NUMBER() OVER(ORDER BY BEGIN_DATE DESC) RN FROM
    WF_ITEMS WHERE ITEM_TYPE = 'xxxxzzzz' AND ROOT_ACTIVITY = 'START_REQUESTS'
    AND SUBSTR(ITEM_KEY,1,INSTR(ITEM_KEY,'-') - 1) = :B1
    ) T WHERE RN <= 1
    call count cpu elapsed disk query current rows
    Parse 0 0.00 0.00 0 0 0 0
    Execute 1 0.00 1.57 0 0 0 0
    Fetch 1 8700.00 544968.73 8180 8185 0 0
    total 2 8700.00 544970.30 8180 8185 0 0
    many thanks

Maybe you are looking for