mySQL Substring(String, Start, Length) 未产生正确的子字符串
我正在尝试获取 HouseAddress EX 的城市子字符串: '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
在这种情况下,城市将为 GOODLETTSVILLE
当我调用子字符串函数
Substring(HouseAddress, Locate(',' , HouseAddress) + 1、长度(OwnerAddress) - 3) AS City FROM housing_data;
它不断产生“GOODLETTSVILLE, TN”,而不仅仅是“GOODLETTSVILLE”。
我什至通过运行 SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-3) AS Test FROM housing_data; 来检查确定 Length(OwnerAddress)-3 是否正在摘录 ', TN' 部分。
我无法找出问题所在,并且已经尝试了一个小时。我只是想知道为什么当我的其他子字符串函数工作正常时这不起作用?
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
I am trying to get the city substring of an HouseAddress EX: '1808 FOX CHASE DR, GOODLETTSVILLE, TN'
In this case the city will be GOODLETTSVILLE
When I call the substring function
Substring(HouseAddress, Locate(',' , HouseAddress) + 1, Length(OwnerAddress) - 3) AS City FROM housing_data;
It keeps resulting in 'GOODLETTSVILLE, TN', rather than just 'GOODLETTSVILLE'.
I even checked sure Length(OwnerAddress)-3 is snippeting the ', TN' part of by running a SELECT SUBSTRING(HouseAddress, LENGTH(HouseAddress)-3) AS Test FROM housing_data;
I can't find out what's wrong and have been trying for an hour. I just want to know why this doesn't work when my other substring functions work fine?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Substring_index,更全面一点。
只有其中一个部分没有昏迷,这才有效
db<>fiddle 此处
Substring_index, is a bit more comprehensive.
Thsi wuld only work, if one of the parts don't have acoma in it
db<>fiddle here
字符串
'1808 FOX CHASE DR, GOODLETTSVILLE, TN'
的长度为37
,因此表达式Length(OwnerAddress) - 3
的计算结果为(37 - 3 =)34
.字符串
'1808 FOX CHASE DR 的子字符串, GOODLETTSVILLE, TN'
在第一个,
之后开始(这是Locate(',' , HouseAddress) + 1
返回的内容)并扩展到下一个33 个字符(因此包含 34 个字符)为' GOODLETTSVILLE, TN'
(少于 34 个字符,因为字符串结尾为我怀疑您认为
Substring(String, Start, Length)
的 3d 参数是 Length,指的是从开头开始的长度字符串的,但事实并非如此。长度从第二个参数开始定义的位置开始。
The length of the string
'1808 FOX CHASE DR, GOODLETTSVILLE, TN'
is37
, so the expressionLength(OwnerAddress) - 3
evaluates to (37 - 3 =)34
.The substring of the string
'1808 FOX CHASE DR, GOODLETTSVILLE, TN'
that starts right after the first,
(this is whatLocate(',' , HouseAddress) + 1
returns) and extends to the next 33 chars (so that 34 chars are included) is' GOODLETTSVILLE, TN'
(less than 34 chars because the end of the string is reached).I suspect that you think that the 3d argument of
Substring(String, Start, Length)
which is Length, refers to the length starting from the beginning of the string, but this is not so.The Length starts from the position defined by the 2nd argument Start.