mySQL 子字符串(字符串、开始、长度)未生成正确的子字符串
mySQL Substring(String, Start, Length) Not Resulting in Correct Substring
我正在尝试获取 HouseAddress EX 的城市子字符串:'1808 FOX CHASE DR, GOODLETTSVILLE, TN'
在这种情况下,城市将是 GOODLETTSVILLE
当我调用子字符串函数时
Substring(HouseAddress, Locate(',', HouseAddress) + 1, Length(OwnerAddress) - 3) AS City FROM housing_data;
它一直导致 'GOODLETTSVILLE, TN',而不仅仅是 'GOODLETTSVILLE'。
我什至检查确定 Length(OwnerAddress)-3 正在截取 运行 a SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-3) AS Test FROM 的 ', TN' 部分housing_data;
我找不到问题所在,已经尝试了一个小时。我只是想知道为什么当我的其他子字符串函数工作正常时这不起作用?
SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-2, LENGTH(HouseAddress)) AS STATE FROM housing_data; -- Retrieves the State Perfectly OK 'TN'
SELECT SUBSTRING(HouseAddress, 1, LOCATE(',', HouseAddress)-1) AS STREET FROM housing_data;
-- Retrieves the Street Perfectly OK '1808 FOX CHASE DR'
SELECT SUBSTRING(HouseAddress, LOCATE(',', HouseAddress) + 1, LENGTH(HouseAddress) -3) AS CITY FROM housing_data; -- Doesn't work, result is GOODLETTSVILLE, TN
Substring_index,比较全面
这只有在其中一个部分没有昏迷的情况下才有效
SET @a = '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
SELECT SUBSTRING_INDEX(@a,',',1),SUBSTRING_INDEX(SUBSTRING_INDEX(@a,',',-2),',',1),SUBSTRING_INDEX(@a,',',-1)
SUBSTRING_INDEX(@a,',',1) | SUBSTRING_INDEX(SUBSTRING_INDEX(@a,',',-2),',',1) | SUBSTRING_INDEX(@a,',',-1)
:------------------------ | :------------------------------------------------ | :-------------------------
1808 FOX CHASE DR | GOODLETTSVILLE | TN
db<>fiddle here
I just want to know why this doesn't work when my other substring
functions work fine?
字符串 '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
的长度为 37
,因此表达式 Length(OwnerAddress) - 3
的计算结果为 (37 - 3 =) 34
.
字符串 '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
的子字符串,紧接在第一个 ,
之后开始(这就是 Locate(',' , HouseAddress) + 1
returns)并延伸到接下来的 33 个字符(所以包括 34 个字符)是 ' GOODLETTSVILLE, TN'
(少于 34 个字符,因为已到达字符串末尾)。
我怀疑你认为Substring(String, Start, Length)
的3d参数是Length,指的是从字符串开头开始的长度,其实这不是所以。
Length 从第二个参数 Start.
定义的位置开始
我正在尝试获取 HouseAddress EX 的城市子字符串:'1808 FOX CHASE DR, GOODLETTSVILLE, TN'
在这种情况下,城市将是 GOODLETTSVILLE
当我调用子字符串函数时
Substring(HouseAddress, Locate(',', HouseAddress) + 1, Length(OwnerAddress) - 3) AS City FROM housing_data;
它一直导致 'GOODLETTSVILLE, TN',而不仅仅是 'GOODLETTSVILLE'。
我什至检查确定 Length(OwnerAddress)-3 正在截取 运行 a SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-3) AS Test FROM 的 ', TN' 部分housing_data;
我找不到问题所在,已经尝试了一个小时。我只是想知道为什么当我的其他子字符串函数工作正常时这不起作用?
SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-2, LENGTH(HouseAddress)) AS STATE FROM housing_data; -- Retrieves the State Perfectly OK 'TN'
SELECT SUBSTRING(HouseAddress, 1, LOCATE(',', HouseAddress)-1) AS STREET FROM housing_data;
-- Retrieves the Street Perfectly OK '1808 FOX CHASE DR'
SELECT SUBSTRING(HouseAddress, LOCATE(',', HouseAddress) + 1, LENGTH(HouseAddress) -3) AS CITY FROM housing_data; -- Doesn't work, result is GOODLETTSVILLE, TN
Substring_index,比较全面
这只有在其中一个部分没有昏迷的情况下才有效
SET @a = '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
SELECT SUBSTRING_INDEX(@a,',',1),SUBSTRING_INDEX(SUBSTRING_INDEX(@a,',',-2),',',1),SUBSTRING_INDEX(@a,',',-1)
SUBSTRING_INDEX(@a,',',1) | SUBSTRING_INDEX(SUBSTRING_INDEX(@a,',',-2),',',1) | SUBSTRING_INDEX(@a,',',-1) :------------------------ | :------------------------------------------------ | :------------------------- 1808 FOX CHASE DR | GOODLETTSVILLE | TN
db<>fiddle here
I just want to know why this doesn't work when my other substring functions work fine?
字符串 '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
的长度为 37
,因此表达式 Length(OwnerAddress) - 3
的计算结果为 (37 - 3 =) 34
.
字符串 '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
的子字符串,紧接在第一个 ,
之后开始(这就是 Locate(',' , HouseAddress) + 1
returns)并延伸到接下来的 33 个字符(所以包括 34 个字符)是 ' GOODLETTSVILLE, TN'
(少于 34 个字符,因为已到达字符串末尾)。
我怀疑你认为Substring(String, Start, Length)
的3d参数是Length,指的是从字符串开头开始的长度,其实这不是所以。
Length 从第二个参数 Start.