是否可以在子字符串函数中使用字符而不是位置?

发布于 2024-09-12 20:42:35 字数 343 浏览 3 评论 0原文

是否可以在子字符串函数中使用字符而不是位置?

SELECT SUBSTRING(title,2) FROM table
puts out every title starting with the second position.

现在我想在空格后剪切输出。空间位置各不相同。 可以实现吗?

我尝试过某事。喜欢

SUBSTRING(title,2,LOCATE('',title))
,but for some reason the output was empty.

先感谢您。 索洛科

is it possible to use characters instead of a position in a substring function?

SELECT SUBSTRING(title,2) FROM table


puts out every title starting with the second position.

Now I want to cut the output after a space. The space positions varies.
Is it realiseable?

I tried sth. like

SUBSTRING(title,2,LOCATE('',title))

,but for some reason the output was empty.

Thank you in advance.
Soloco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-09-19 20:42:35

您可以使用 SUBSTRING_INDEX 来实现:

SELECT SUBSTRING_INDEX(title, ' ', 1) FROM table
SELECT SUBSTRING_INDEX('hello world', ' ', 1) # gives you 'hello'

http://dev.mysql.com/doc/refman/5.0/en/string-functions.htm

You can use SUBSTRING_INDEX for that:

SELECT SUBSTRING_INDEX(title, ' ', 1) FROM table
SELECT SUBSTRING_INDEX('hello world', ' ', 1) # gives you 'hello'

http://dev.mysql.com/doc/refman/5.0/en/string-functions.htm

2024-09-19 20:42:35

在 LOCATE 语句中,您要搜索空字符串 '' 而不是空格 ' '

添加空格字符。

您所做的操作将从字符串的第二个字符到第一次出现的空格字符之间获取字符串。这真的是你想做的吗?如果第一个或第二个字符是空格字符怎么办?

In your LOCATE statement, you are searching for an empty string '' instead of a space ' '

Add a space character.

What you are doing will fetch the string from its second character to the first occurrence of a space character. Is this really what you want to do? What if the the first or second character are space characters?

茶花眉 2024-09-19 20:42:35

您可以使用:

SUBSTRING(title,2,charindex('',title)-1)

这会在标题中的空格之前剪切字符串

You could use:

SUBSTRING(title,2,charindex('',title)-1)

This would cut the string just before the space in the title

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文