How to separate a letter from a Character in SQL?

Asked

Viewed 159 times

1

I have a variable of type Character Varying(17) and I need to separate one of the characters, for example the third letter, and use it to filter.
For example:

SELECT *
FROM fnord
WHERE terceira(illuminati) = "a"

2 answers

2


I think the simplest will be (using the 3rd letter hint):

SELECT  *
FROM    fnord
WHERE   substr('illuminati', 3, 1) = 'a'

The first parameter is a string that you want to validate, 2º is the index and 3º is the number of characters you want to "remove" (is optional).

  • Only one detail: the third parameter if unspecified is equivalent to all the remaining characters of the string. See: http://sqlfiddle.com/#! 17/9eecb/18657

  • Right, so I indicated that it would be optional.

2

For that there is the function replace

substr(string, from [, Count])
substr('Alphabet', 3, 1) returns p (third position , a character)

See working on Sqlfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.