oracle 如何获取字符串的最后一个字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13220874/
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
How to get the last letter of a string
提问by Dwayne Radar
Hi guys just wanted to ask how I can get the last letter of a string and check if it's vowel or consonant. by the way Im using oracle 10g. thank you for those who will help me out. peacE!
嗨,伙计们,我只是想问我如何获得字符串的最后一个字母并检查它是元音还是辅音。顺便说一句,我使用oracle 10g。感谢那些愿意帮助我的人。和平!
here's what I came up with already:
这是我已经想到的:
SELECT last_name,
Substr(last_name, -1, 1) "Last letter",
Substr(last_name, 1, 1) "First letter",
CASE
WHEN Substr(last_name, -1, 1) IN ( 'a', 'e', 'i', 'o', 'u' ) THEN
'ends with a vowel'
WHEN Substr(last_name, -1, 1) IN ( 'b', 'c', 'd', 'f',
'g', 'h', 'j', 'k',
'l', 'm', 'n', 'p',
'q', 'r', 's', 't',
'v', 'w', 'x', 'y', 'z' ) THEN
'ends with a consonant'
END "Last Letter Description",
CASE
WHEN Substr(last_name, 1, 1) IN ( 'a', 'e', 'i', 'o', 'u' ) THEN
'starts with a consonant'
WHEN Substr(last_name, 1, 1) IN ( 'b', 'c', 'd', 'f',
'g', 'h', 'j', 'k',
'l', 'm', 'n', 'p',
'q', 'r', 's', 't',
'v', 'w', 'x', 'y', 'z' ) THEN
'starts with a consonant'
END "First Letter Description"
FROM employees
GROUP BY first_name,
last_name
now when you execute this on oracle 10g the "First Letter Description" is empty! Help me please what do you think is wrong with my code?
现在,当您在 oracle 10g 上执行此操作时,“第一个字母描述”为空!请帮助我,您认为我的代码有什么问题?
回答by Jo?o Barreto
Try this, not complete, but with easy adjustments you can make it work the way you want:
试试这个,不完整,但通过简单的调整,你可以让它按照你想要的方式工作:
FUNCTION last_is_vowel (string_in VARCHAR2)
RETURN BOOLEAN
IS
BEGIN
RETURN CASE WHEN LOWER(SUBSTR(string_in, -1)) IN ('a', 'e', 'i', 'o', 'u')
THEN TRUE
ELSE FALSE
END;
END last_is_vowel;
回答by danimal
Look at your data. Chances are the first character in employees.last_name is capitalized. Remember, Oracle is case sensitive. You can user UPPER() or LOWER() to help find your match.
看看你的数据。机会是雇员中的第一个字符。last_name 是大写的。请记住,Oracle 区分大小写。您可以使用 UPPER() 或 LOWER() 来帮助找到您的匹配项。
Also it'd be more efficient to search just for vowels and use an else statement for to find exclusions as Jo?o suggests.
此外,如 Jo?o 建议的那样,仅搜索元音并使用 else 语句来查找排除项会更有效。
SELECT last_name,
Substr(last_name, -1, 1) "Last character",
Substr(last_name, 1, 1) "First character",
CASE
WHEN lower(Substr(last_name, -1, 1)) IN ( 'a', 'e', 'i', 'o', 'u' ) THEN
'ends with a vowel'
ELSE
'does not end with a vowel'
END "Last Letter Description",
CASE
WHEN lower(Substr(last_name, 1, 1)) IN ( 'a', 'e', 'i', 'o', 'u' ) THEN
'starts with a vowel'
ELSE
'does not start with a vowel'
END "First Letter Description"
FROM employees
GROUP BY first_name,
last_name