用于选择前 3 个字符的 Sql 查询 [英] Sql query for selecting the first 3 characters
本文介绍了用于选择前 3 个字符的 Sql 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 2 个表中选择记录,其中名为 DESC 的第一个表列(前 3 个字符)应与第二个表的项目列匹配.
I am trying to select the records from 2 tables in which the 1st table column named DESC (first 3 characters) should match with the project column of the 2nd table.
select SUBSTRING(a.[DESC],1,3) from Table1 a
left outer join Table2 b
on a.[DESC] = b.project
where SUBSTRING(a.[DESC],1,3) like b.project
输入:第一个表 DESC 列:值:'2AB F YY'
Input: 1st Table DESC Column: Value: '2AB F YY'
第二表项目列:值:'2AB'
2nd Table Project Column: Value: '2AB'
预期输出:返回所有值为 2AB 的记录
Expected Output: Return all the records of value 2AB
推荐答案
请避免使用 DESC 等保留关键字
MS SQL Server 2017 架构设置:
CREATE TABLE Table1 ([DESC] varchar(255))
CREATE TABLE Table2 (Project varchar(255))
INSERT INTO Table1([DESC])values('2AB F YY')
INSERT INTO Table2(Project)values('2AB')
查询 1:
select SUBSTRING(a.[DESC],1,3)
from Table1 a
join Table2 b
on SUBSTRING(a.[DESC],1,3) = b.project
结果:
| |
|-----|
| 2AB |
这篇关于用于选择前 3 个字符的 Sql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文