我有一个表,我想编写查询来获取数据
empno empname
1 abc
2 xyz
3 mnc
4 pqr
现在我想编写查询那些未在empname
中用作首字母的字母,如
B,C,d,E,F,G,H,I,J,K,L,N,O,Q,R,S,T,U,V,W,Y,Z
我不需要在empname
A,X,M,P
那么我该如何为此编写查询?
提前致谢
答案 0 :(得分:2)
首先使用表变量存储下面的所有字母表。
jdbc.username=sa
jdbc.password=123456
然后使用declare @alphabets as table(letter varchar(1));
insert into @alphabets values
('a'),
('b'),
('c'),
('d'),
('e'),
('f'),
('g'),
('h'),
('i'),
('j'),
('k'),
('l'),
('m'),
('n'),
('o'),
('p'),
('q'),
('r'),
('s'),
('t'),
('u'),
('v'),
('w'),
('x'),
('y'),
('z');
从表变量中找到缺少的字母,其中第一个字符为NOT EXISTS
,并使用empname
以逗号连接所有字符。
<强>查询强>
STUFF
Demo
强> 答案 1 :(得分:1)
使用以下查询
select * from employee
where empname not like 'a%'
and empname not like 'x%'
and empname not like 'm%'
and empname not like 'p%'
这里&#39; s%&#39;用于选择名称的第一个字符。