[英]Using 'LIKE' in a CASE statement
I am attempting to generate a new field based on an existing field where some of the entries contain different special characters. 我试图根据现有字段(其中某些条目包含不同的特殊字符)生成一个新字段。 (ie. *, ') The special characters are at the end of the string, which is either second or third position. (即*,')特殊字符位于字符串的末尾,即第二或第三位置。
I am NEW to SQL but not new to data. 我对SQL不熟悉,但对数据却不陌生。 I am using a CASE WHEN statement. 我正在使用CASE WHEN语句。 I have tried several approaches and several other commands within the CASE statement. 我在CASE语句中尝试了几种方法和其他一些命令。
what I want is: 我想要的是:
SELECT *
CASE WHEN grde_code_mid LIKE '[*]' THEN 'Remedial'
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble
I keep getting the same error: "FROM keyword not found where expected". 我不断收到相同的错误:“未在预期的位置找到FROM关键字”。 I expect to have all 3 returns in the new field. 我希望所有3个回报都在新字段中。
If you're looking for ' character you should escape it. 如果您要查找'字符,则应将其转义。
Change 更改
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
by 通过
WHEN grde_code_mid LIKE '['']' THEN 'Continuing'
Have a look at this question: How do I escape a single quote in SQL Server? 看一下这个问题: 如何在SQL Server中转义单引号?
There are several issues with your query: 您的查询存在几个问题:
SELECT
clause between *
and the CASE
expression (this is causing the error that you are currently getting) 您在*
和CASE
表达式之间的SELECT
子句中缺少逗号(这导致您当前得到的错误) *
in a portable manner, you need expression ... LIKE '%*'
, not LIKE '[*]'
如果要以可移植的方式匹配以*
结尾的字符串,则需要表达式... LIKE '%*'
,而不是LIKE '[*]'
SELECT *, other_field FROM ...
is not supported on all RDBMS (and, as commented by jarhl , actually isn't standard ANSI SQL notation); 并非在所有RDBMS上都支持SELECT *, other_field FROM ...
(并且正如jarhl所评论的那样 ,实际上不是标准的ANSI SQL表示法); you usually need to prefix the *
with the table name or alias 您通常需要在*
加上表名或别名 Consider: 考虑:
SELECT
g.*,
CASE
WHEN grde_code_mid LIKE '%*' THEN 'Remedial'
WHEN grde_code_mid LIKE '%''' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble g
Thank you all. 谢谢你们。 I am still getting the hang of the language and proper terms. 我仍然掌握该语言和适当术语。 I am using Oracle DB. 我正在使用Oracle DB。 Also, yes, I did need to reference the table in the select statement (SELECT tablename .*). 另外,是的,我确实需要在select语句(SELECT tablename 。*)中引用该表。 Found a work around: 找到了一个解决方法:
CASE WHEN regexp_like(grde_code_mid, '[*]') THEN 'Remedial' WHEN regexp_like(grde_code_mid, '['']') THEN 'Continuing' ELSE NULL END AS special_class regexp_like(grde_code_mid,'[*]')THEN'Remedial'的情况regexp_like(grde_code_mid,'[“]')THEN'Continueing'ELSE NULL END AS special_class的情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.