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);

智能推荐

注意!

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



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

赞助商广告