How do I find the position of a character in a SQL

2020-08-14 09:37发布

If the result "joe@stackoverflow.com" and I want to find the position of the @ symbol (3). Is it possible? It does not appear that SQLite has the equivalent of INSTR, LOCATE, POSITION or any such function.

SELECT ?('joe@stackoverflow.com')

Update I ended up registering a custom extension function, but was surprised that I had to.

标签: sql sqlite
9条回答
叼着烟拽天下
2楼-- · 2020-08-14 10:31

Position of char '@' can be finded by function CHARINDEX (MSSQL):

SELECT CHARINDEX('@', 'joe@stackoverflow.com', 0)
查看更多
地球回转人心会变
3楼-- · 2020-08-14 10:33

Got this from a spiceworks posting:

The first step is to remove all characters up to a specific character using ltrim or rtrim. If you're retrieving a filename then you trim all characters that are not slashes. If you're retrieving a username then you trim everything that is not an @ character. You can then use that trimmed string in a replace function to replace its occurrence with an empty string. That will provide you with every character that you just trimmed in the first step. In other words, the username or filename.

create table example (path varchar(256), email varchar(128));

insert into example values ('/path/to/file1.txt', 'user@domain.com');

Retrieve user from email:

select replace(email, ltrim(email, '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!#$%^&*()_+-=`~[]\/{}|;:,.<>?'),'') from example;
查看更多
beautiful°
4楼-- · 2020-08-14 10:34

Retrieve filename from a path:

select replace(path, rtrim(path, replace(path, '/', '' ) ), '') from example;

Got from http://community.spiceworks.com/topic/38836-sqlite-question-how-do-i-find-the-position-of-a-character-in-a-string?page=2, hope it could help someone.

查看更多
登录 后发表回答