美文网首页数据库数据仓库DW数据仓库
一句SQL,判断char列的值是否组成回文字符串

一句SQL,判断char列的值是否组成回文字符串

作者: 033a1d1f0c58 | 来源:发表于2017-04-15 09:32 被阅读57次

Table t has 2 columns:
id INT;
value CHAR(1);
Column id starts from 0, increased by 1 each row
Column value is a single character string
Table t has at least 1 row
String s is a palindrome when:
s[i] = s[s.length - i - 1] for i = 0 … (s.length – 1) / 2
E.g.: a, aba, abba.
Q: Write one SQL statement to check if the string composed of value of t ordered by id is a palindrome (case sensitive).
Output “Y” or “N”.


这里写图片描述
with tmp as 
(
    (select (select count(*) from t1)-1-id as id,value from t1)
except
    (select id,value from t1)
)
select 
case when count(*)=0 
then 'Y' else 'N' end 
from tmp;

相关文章

网友评论

    本文标题:一句SQL,判断char列的值是否组成回文字符串

    本文链接:https://www.haomeiwen.com/subject/qtrnattx.html