Maoritzio
Maoritzio

Reputation: 1232

SQLite: trim usage

My goal is to extract the domain out of given URL.

For that end I use the following:

select distinct ltrim(rtrim('https://www.youtube.com/watch?v=...', '/'), 'https://')

The result I get is:

www.youtube.com/watch?v=...

While the following is expected:

www.youtube.com

How can the above be achieved?

Note:

I notices that the trim function works differently than I expected.

Any explainations are welcomed.

Upvotes: 0

Views: 370

Answers (2)

Maoritzio
Maoritzio

Reputation: 1232

At the end I didn't use trim but substr as offered.

The following worked:

select replace(substr(substr(<url>, instr(<url>, '//')+2),0,instr(substr(<url>, instr(<url>, '//')+2),'/')),'.','')

select replace(substr(substr(<url>, instr(<url>, '//www.')+6),0,instr(substr(<url>, instr(<url>, '//www.')+6),'/')),'.','')

Upvotes: 0

Rhapsody
Rhapsody

Reputation: 6077

Trim only removes the given characters at the beginning and/or end of the string. You'll need substr and instr. (https://www.sqlite.org/lang_corefunc.html)

But the best option is probably to fix this in your code before saving it to the database.

Upvotes: 2

Related Questions