日期:2014-05-17  浏览次数:20571 次

求一SQL查找语句
字段中有如下内容

23233-34
1121-34A
4343-3455-34B
G332-34C
HGF-231-45A
HGD-P1-P2-54B
kkj-weweew-34A
KLI-O2-P4-54D
HUI-we-rre-34B

要求条件:按最后结尾字符仅仅输入:34 
则得到如下的记录 (即遇上最后一位如果是字母a-z 或A-Z的则判断最后结尾字符是排除这些字母来计算)

23233-34
1121-34A
4343-3455-34B
G332-34C
kkj-weweew-34A
HUI-we-rre-34B

------解决方案--------------------
SQL code
--> 测试数据:[tb]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO 
CREATE TABLE [tb]([name] VARCHAR(14))
INSERT [tb]
SELECT '23233-34' UNION ALL
SELECT '1121-34A' UNION ALL
SELECT '4343-3455-34B' UNION ALL
SELECT 'G332-34C' UNION ALL
SELECT 'HGF-231-45A' UNION ALL
SELECT 'HGD-P1-P2-54B' UNION ALL
SELECT 'kkj-weweew-34A' UNION ALL
SELECT 'KLI-O2-P4-54D' UNION ALL
SELECT 'HUI-we-rre-34B'
--------------开始查询--------------------------

SELECT * FROM [tb] WHERE name LIKE '%34' OR name LIKE '%34[a-z]'
----------------结果----------------------------
/* 
name
--------------
23233-34
1121-34A
4343-3455-34B
G332-34C
kkj-weweew-34A
HUI-we-rre-34B

(6 行受影响)

*/