Informix 7.3 中是否有内置函数可以测试字母或数字数据?

发布于 2024-10-28 06:14:11 字数 1739 浏览 2 评论 0原文

我一直在寻找一个 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) >、TRIMTRANSLATE,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-11-04 06:14:11

这是我发现的一种测试 alpha 的方法。这是一种解决方法,但我认为到目前为止它是可靠的。

SELECT phone FROM given_table WHERE UPPER(phone)<>LOWER(phone);

参考: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.

SELECT phone FROM given_table WHERE UPPER(phone)<>LOWER(phone);

Reference: http://www.geekinterview.com/question_details/34549 (see answer from the user "amarnathtnl" a little more than halfway down the page)

逆夏时光 2024-11-04 06:14:11

这工作可靠:

CREATE FUNCTION to_int(alpha VARCHAR(16)) RETURNING INT;
  DEFINE n INT;
  ON EXCEPTION IN (-1213) RETURN NULL; END EXCEPTION;
  LET n = alpha;
  RETURN n;
END FUNCTION;

它产生数字或 NULL。

This works reliably:

CREATE FUNCTION to_int(alpha VARCHAR(16)) RETURNING INT;
  DEFINE n INT;
  ON EXCEPTION IN (-1213) RETURN NULL; END EXCEPTION;
  LET n = alpha;
  RETURN n;
END FUNCTION;

It yields numeric ot NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文