• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Short Db Field

Participant ,
Jan 22, 2007 Jan 22, 2007

Copy link to clipboard

Copied

Hi..I have a db field called "sheetNo" it contains smth like "1,2,3,4,5,Cover,1B,2C,9,20,21 "

When short it by Asc or desc in my query, it doenst sort...what might be wrong ??

Thank u all..

TOPICS
Advanced techniques

Views

487

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2007 Jan 22, 2007

Copy link to clipboard

Copied

The problem is that you are storing numbers as text. When you do that '9' is greater than '10' .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2007 Jan 22, 2007

Copy link to clipboard

Copied

The problem is that you are storing numbers as text. When you do that '9' is greater than '10' .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 22, 2007 Jan 22, 2007

Copy link to clipboard

Copied

Depending on your database, you may be able to perform some gymnastics using CASE and functions to give you a sort order that is more in line with your expectations.

Example:

SELECT sheetNo
FROM your_table
WHERE whatever
ORDER BY CASE
WHEN IsNumeric(sheetNo) <> 0
THEN CAST(sheetNo AS NUMERIC)
ELSE 0 END,
UPPER(sheetNo)

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2007 Jan 22, 2007

Copy link to clipboard

Copied

I will try

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 23, 2007 Jan 23, 2007

Copy link to clipboard

Copied

I am using oracle..I think isnumeric is not an oracle function..what is the equivalent..

ORDER BY CASE WHEN IsNumeric(ctrDocs.sheetno)<> 0 THEN
cast(ctrDocs.sheetno AS NUMERIC) ELSE 0 END

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 23, 2007 Jan 23, 2007

Copy link to clipboard

Copied

to_number

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 23, 2007 Jan 23, 2007

Copy link to clipboard

Copied

I m still getting err.. What I am doing wrong..it is oracle..
ORDER BY CASE WHEN TO_NUMBER(ctrDocs.sheetno)<> 0 THEN
cast(ctrDocs.sheetno AS NUMERIC) ELSE 0 END

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 23, 2007 Jan 23, 2007

Copy link to clipboard

Copied

Ah yes, Oracle. The to_number() function will throw an Oracle ORA-01722: invalid number error if you try to use it on anything non-numeric, so it really can't be used in this way because you have no way to trap the exception. Since there is no "native" isnumeric() function, you have to work around...

Borrowing from Oracle/PLSQL: Test a string for a numeric value, something like this might get you in the ballpark...

SELECT sheetNo
FROM ctrDocs
WHERE whatever
ORDER BY CASE
WHEN LENGTH(TRIM(TRANSLATE(ctrDocs.sheetno, '.0123456789',' '))) IS NULL
THEN CAST(ctrDocs.sheetno AS NUMBER)
ELSE 0 END, ctrDocs.sheetno

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 23, 2007 Jan 23, 2007

Copy link to clipboard

Copied

LATEST
Thank you all.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation