SQL IF 值 LIKE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16357642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:05:44  来源:igfitidea点击:

SQL IF value LIKE

sqlif-statementsql-like

提问by

I'm new to SQL, so apologies for what is probably a menial question.

我是 SQL 的新手,因此对于可能是一个琐碎的问题深表歉意。

I'm storing staff numbers in a format similar to ‘e01, e02 .. e98, e99'

我正在以类似于的格式存储员工编号 ‘e01, e02 .. e98, e99'

I have a procedure to input values into the employee table that I only want to be carried out if the provided value for the staff number field is the same format as shown above.

我有一个将值输入到员工表中的过程,如果为员工编号字段提供的值与上面显示的格式相同,我只想执行该过程。

I have tried ..

我试过了 ..

CREATE PROCEDURE new_staff (IN e_id employee ids,
                ….,
                ….,etc)
BEGIN
IF e_id not like 'e__'
   THEN /* fail*/

ELSE /* insert record*/
END

I tested this using the value ‘e9999' and it didn't recognise the error, it instead just took the value 'e99'. Any help would be much appreciated.

我使用值“e9999”对此进行了测试,但它没有识别出错误,而只是采用了值“e99”。任何帮助将非常感激。

Many thanks

非常感谢

-Edit- Sorry - To clear things up. I'm using SQL Anywhere and I want the maximum possible employee id to be e99. My logic was that using like e__(two underscores) would make it impossible to input e100 or higher.

- 编辑 - 对不起 - 把事情弄清楚。我正在使用 SQL Anywhere,并且希望最大可能的员工 ID 为 e99。我的逻辑是使用like e__(两个下划线)会使输入e100 或更高版本变得不可能。

回答by Obl Tobl

EDIT:

编辑:

Try it the other way round

反过来试试

IF e_id like 'e__'  
   THEN 
   /* insert record*/  
ELSE   
  /* fail*/
END

If you want the wildcards to be only numbers, try this one:

如果您希望通配符仅为数字,请尝试以下方法:

IF e_id like 'e[0-9][0-9]'  
   THEN 
   /* insert record*/  
ELSE   
  /* fail*/
END

For further information on SQL-Wildcards visit this link.

有关 SQL 通配符的更多信息,请访问此链接

回答by Milen

Try using global symbols (e%):

尝试使用全局符号 (e%):

CREATE PROCEDURE new_staff     (IN e_id employee ids,   
      ….,
                ….,etc)
BEGIN
IF e_id not like 'e%'
   THEN /* fail*/

ELSE /* insert record*/
END

回答by ThatGuy

Parse everything after 'e' and see if the value is <100. or Add a column that supports integers and one column for the letter prefix - assuming that the e prefix is code for something and that it could be something besides e. when returning the value just create a string with the prefix and add to it the string representation of your integer.

解析 'e' 之后的所有内容,看看值是否 <100。或添加一列支持整数和一列用于字母前缀 - 假设 e 前缀是某物的代码,并且它可能是 e 之外的某物。返回值时,只需创建一个带有前缀的字符串,并向其中添加整数的字符串表示形式。