What's an appropriate way to do startswith(expression)
in SQL?
I can do it with LIKE ((expression) || '%')
, but it doesn't look very nice to me.
Full query is in form:
SELECT …, (SELECT COUNT(*) FROM post AS child WHERE child.path LIKE (post.path || '%') AND child.depth >= post.depth) FROM post WHERE …
I suppose it is preferable to use LIKE
because of DB indexing for this case.
Just use LIKE 'input%'
. I.E:
WHERE child.path LIKE post.path + '%'
(I assume this is for SQL Server, though this syntax probably works elsewhere)
In standard SQL, you can also say:
where position(post.path in child.path) = 1
I don't know if your RDBMS supports that. PostgreSQL does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With