在两个 table 连接中从 Like 语句中转义方括号
Escape square brackets from Like statement in two table joining
在比较带有方括号的数据时 Ex: '-[VVE172BUS-3.30]-' 我在下面使用
Select * 来自 ServerOps.dbo.tbl_VmHealth_Firmware
其中 biosname 像 '-[[]VVE172BUS-3.30]-'
我得到了正确的结果,但是我需要将 biosname '-[VVE172BUS-3.30]-' 与来自不同 table 的另一列进行比较。你能帮我在加入两列时如何逃避[]吗
SELECT VmhostName, ParentName, DataCenterName,VcenterName,
VF.Manufacturer, VF.Model, VF.BiosVersion, MAX(VF.ReleaseId) AS ReleaseId
FROM ServerOps.dbo.tbl_VmHealth_Vmhost VH WITH (NOLOCK)
LEFT JOIN ServerOps.dbo.tbl_VmHealth_Firmware VF WITH (NOLOCK)
ON VH.Manufacturer LIKE '%' + VF.Manufacturer + '%' AND VH.Model = VF.Model
AND VH.BiosVersion LIKE '%' + VF.BiosVersion + '%'
AND ((VH.ReleaseDate >= VF.ReleaseDate AND VH.Manufacturer not like '%Dell%')
OR (VH.Manufacturer like '%Dell%'))
WHERE (VcenterName IS NOT NULL)
GROUP BY VmhostName, ParentName, DataCenterName, VcenterName, VF.Manufacturer, VF.Model, VF.BiosVersion, VF.BiosVersion
方括号问题出现在 SQL 服务器中,并且只出现在左括号 [
中。这是因为我们可以使用方括号包围标识符,例如 table 和列名,这允许我们使用带空格的名称没有任何问题,但这意味着 SQL 服务器将左方括号视为使用 LIKE 时的特殊字符。
--CREATE TWO IDENTICAL TABLES AND INSERT SOME DATA
CREATE TABLE t1 ( biosname VARCHAR(100));
CREATE TABLE t2 ( biosname VARCHAR(100));
INSERT INTO t1 VALUES ('-[VVE172BUS-3.30]-');
INSERT INTO t2 VALUES ('-[VVE172BUS-3.30]-');
--WITH THE OPERATEUR = ALL IS WELL
SELECT * FROM t1 WHERE biosname = '-[VVE172BUS-3.30]-';
| biosname |
| :----------------- |
| -[VVE172BUS-3.30]- |
--BUT WITH LIKE WE GET NO RESULT
SELECT * FROM t1 WHERE biosname LIKE '-[VVE172BUS-3.30]-';
| biosname |
| :------- |
--WE CAN USE AN ESCAPE CHARACTER IF WE DECLARE IT
SELECT * FROM t1 WHERE biosname LIKE '-\[VVE172BUS-3.30]-' ESCAPE '\';
| biosname |
| :----------------- |
| -[VVE172BUS-3.30]- |
--THE SAME RULES APPLY FOR COMPARING 2 FIELDS
--ALL IS WELL IF WE USE =
Select * from t1, t2
where t1.biosname = T2.biosname;
biosname | biosname
:----------------- | :-----------------
-[VVE172BUS-3.30]- | -[VVE172BUS-3.30]-
--TO USE ESCAPE LIKE WE HAVE THE REPLACE '[' WITH '\['
Select * from t1, t2
where t1.biosname like REPLACE(t2.biosname, '[', '\[') escape '\';
biosname | biosname
:----------------- | :-----------------
-[VVE172BUS-3.30]- | -[VVE172BUS-3.30]-
db<>fiddle here
在比较带有方括号的数据时 Ex: '-[VVE172BUS-3.30]-' 我在下面使用
Select * 来自 ServerOps.dbo.tbl_VmHealth_Firmware 其中 biosname 像 '-[[]VVE172BUS-3.30]-'
我得到了正确的结果,但是我需要将 biosname '-[VVE172BUS-3.30]-' 与来自不同 table 的另一列进行比较。你能帮我在加入两列时如何逃避[]吗
SELECT VmhostName, ParentName, DataCenterName,VcenterName,
VF.Manufacturer, VF.Model, VF.BiosVersion, MAX(VF.ReleaseId) AS ReleaseId
FROM ServerOps.dbo.tbl_VmHealth_Vmhost VH WITH (NOLOCK)
LEFT JOIN ServerOps.dbo.tbl_VmHealth_Firmware VF WITH (NOLOCK)
ON VH.Manufacturer LIKE '%' + VF.Manufacturer + '%' AND VH.Model = VF.Model
AND VH.BiosVersion LIKE '%' + VF.BiosVersion + '%'
AND ((VH.ReleaseDate >= VF.ReleaseDate AND VH.Manufacturer not like '%Dell%')
OR (VH.Manufacturer like '%Dell%'))
WHERE (VcenterName IS NOT NULL)
GROUP BY VmhostName, ParentName, DataCenterName, VcenterName, VF.Manufacturer, VF.Model, VF.BiosVersion, VF.BiosVersion
方括号问题出现在 SQL 服务器中,并且只出现在左括号 [
中。这是因为我们可以使用方括号包围标识符,例如 table 和列名,这允许我们使用带空格的名称没有任何问题,但这意味着 SQL 服务器将左方括号视为使用 LIKE 时的特殊字符。
--CREATE TWO IDENTICAL TABLES AND INSERT SOME DATA CREATE TABLE t1 ( biosname VARCHAR(100)); CREATE TABLE t2 ( biosname VARCHAR(100)); INSERT INTO t1 VALUES ('-[VVE172BUS-3.30]-'); INSERT INTO t2 VALUES ('-[VVE172BUS-3.30]-');
--WITH THE OPERATEUR = ALL IS WELL SELECT * FROM t1 WHERE biosname = '-[VVE172BUS-3.30]-';
| biosname | | :----------------- | | -[VVE172BUS-3.30]- |
--BUT WITH LIKE WE GET NO RESULT SELECT * FROM t1 WHERE biosname LIKE '-[VVE172BUS-3.30]-';
| biosname | | :------- |
--WE CAN USE AN ESCAPE CHARACTER IF WE DECLARE IT SELECT * FROM t1 WHERE biosname LIKE '-\[VVE172BUS-3.30]-' ESCAPE '\';
| biosname | | :----------------- | | -[VVE172BUS-3.30]- |
--THE SAME RULES APPLY FOR COMPARING 2 FIELDS --ALL IS WELL IF WE USE = Select * from t1, t2 where t1.biosname = T2.biosname;
biosname | biosname :----------------- | :----------------- -[VVE172BUS-3.30]- | -[VVE172BUS-3.30]-
--TO USE ESCAPE LIKE WE HAVE THE REPLACE '[' WITH '\[' Select * from t1, t2 where t1.biosname like REPLACE(t2.biosname, '[', '\[') escape '\';
biosname | biosname :----------------- | :----------------- -[VVE172BUS-3.30]- | -[VVE172BUS-3.30]-
db<>fiddle here