Informix 7.3 中是否有内置函数可以测试字母或数字数据?
我一直在寻找一个 SQL 函数来测试一个值是字母还是数字。 Informix 7.3 似乎没有任何类似的功能,但我可能错过了一些东西或者一直在寻找错误的东西。
例如,我正在尝试测试电话号码字段中是否仅包含数字并进行相应处理:
给定此表,电话字段类型为 char(12):
id name phone
5164 Cheese 973-153-5149
8843 Queso (201)8011830
8356 Formaggio 2129182943
8938 Ost ext.21438
3852 Ser 973-15-1549
我想做像这样的东西:
SELECT CASE WHEN (ISALPHA((TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))))
THEN 'has alpha chars'
WHEN (LENGTH((TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))) NOT IN (10,11))
THEN 'wrong length'
WHEN (phone IN ('',' ',NULL)))
THEN 'phone is blank'
ELSE (TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))
END phone
FROM given_table;
基本上,我试图删除任何 -
, (
, )
, ,
.
电话号码中的字符,修剪该结果,测试其长度是否为 10 或 11 个字符,并确保在所有替换和修剪后数据中没有字母字符。 (如果您觉得我的替换和修剪方式有任何问题,我也愿意就这方面提出建议。正则表达式?)
我想要的最终结果是:
phone
9731535149
2018011830
2129182943
has alpha chars
wrong length
我读过一篇建议使用 LENGTH
解决方案 (Oracle) >、TRIM
和 TRANSLATE
,但 Informix 7.3 不支持 TRANSLATE
*Please note - I will write my own function if none exist, so please do not suggest that as an answer. I just want to know if there are any built-in ways for doing this.
Jonathan Leffler to the rescue?
I have been searching around for a SQL function that tests a value for being alpha or for being numeric. There doesn't seem to be any functions like this for Informix 7.3, but I might have missed something or have been searching for the wrong stuff.
For example, I'm trying to test a phone number field for having only numbers in it and dealing with it accordingly:
Given this table with the phone field type being char(12)
:
id name phone
5164 Cheese 973-153-5149
8843 Queso (201)8011830
8356 Formaggio 2129182943
8938 Ost ext.21438
3852 Ser 973-15-1549
I want to do something like this:
SELECT CASE WHEN (ISALPHA((TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))))
THEN 'has alpha chars'
WHEN (LENGTH((TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))) NOT IN (10,11))
THEN 'wrong length'
WHEN (phone IN ('',' ',NULL)))
THEN 'phone is blank'
ELSE (TRIM(REPLACE((REPLACE((REPLACE((REPLACE((REPLACE(phone,'-','')),'(','')),')','')),' ','')),'.','')))
END phone
FROM given_table;
Basically, I'm trying to remove any -
, (
, )
, ,
.
chars from the phone number, trim that result, test it's length for being either 10 or 11 characters long, and making sure that there is no alpha chars in the data after all the replaces and trim. (If you feel that anything is wrong with how I'm going about replacing & trimming, I'm open for suggestions on that aspect as well. Regex?)
The end result that I would want would be:
phone
9731535149
2018011830
2129182943
has alpha chars
wrong length
I've read one solution (Oracle) that suggests using a combination of LENGTH
, TRIM
, and TRANSLATE
but TRANSLATE
is not supported for Informix 7.3
*Please note - I will write my own function if none exist, so please do not suggest that as an answer. I just want to know if there are any built-in ways for doing this.
Jonathan Leffler to the rescue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我发现的一种测试 alpha 的方法。这是一种解决方法,但我认为到目前为止它是可靠的。
参考:http://www.geekinterview.com/question_details/34549(参见用户的回答“amarnathtnl”位于页面一半以上)
Here is a way I found to test for alpha. It's a work-around, but I think it is reliable so far.
Reference: http://www.geekinterview.com/question_details/34549 (see answer from the user "amarnathtnl" a little more than halfway down the page)
这工作可靠:
它产生数字或 NULL。
This works reliably:
It yields numeric ot NULL.