Firebird確定字符串是否都是數字

[英]Firebird determine if a string is all numbers


I have a VARCHAR field in a Firebird 2.0 table that can contain alphanumeric characters. I need to sort data on this field, sorting all values that contain only numbers as numbers, and sort all other values as 0.

我在Firebird 2.0表中有一個VARCHAR字段,可以包含字母數字字符。我需要對此字段的數據進行排序,將僅包含數字的所有值排序為數字,並將所有其他值排序為​​0。

For example, if I have four values,

例如,如果我有四個值,

"1", "2", "10", "string", 

I need to sort it as

我需要將其排序為

"string", "1", "2", "10". 

Default sort with string sorts as

字符串排序為的默認排序

"1", "10", "2", "string".

I was thinking of casting the values to INTEGER, but I'm getting conversion error on strings, which is of course correct. How to work around this?

我正在考慮將值轉換為INTEGER,但我在字符串上遇到轉換錯誤,這當然是正確的。如何解決這個問題?

2 个解决方案

#1


You can use builtin function LPAD:

你可以使用內置函數LPAD:

SELECT
  ...
  <number_field>,
  ...
FROM
  ...
ORDER BY
  LPAD(<numer_field>, 10)

#2


Create extra column where you store sortable values using your application. Then do sorting based on that column. If you want your numbers to be at the end then insert "ZZZ" (or "ÜÜÜ" or whatever is last character in your language) in front of numbers. Like Format("ZZZ%012d", my_num);

創建額外的列,使用您的應用程序存儲可排序的值。然后根據該列進行排序。如果您希望您的數字在最后,請在數字前插入“ZZZ”(或“ÜÜÜ”或您語言中的最后一個字符)。像格式(“ZZZ%012d”,my_num);


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/09/720060a56ca8203a09c2f7fbe3408010.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com