tags:

views:

41

answers:

2

I have a table

doctors (
id int,
name varchar(255)
)

where name like "Sername Name".

In query i need only Sername of doctor. How can i do it with standard or with solution on most RDBMS?

I know only two solution.

First is SUBSTRING(input SIMILAR pattern ESCAPE escape-char) in postgres is SUBSTRING(input FROM pattern-string).

Second is in postgres like substring(name, 0 , position(' ').

UPD: Is it normal to ask rdbms to split string or better do it manualy in code?

+1  A: 

See http://sqlnut.atw.hu/sqlnut2-chp-4-sect-4.html

Search for substring and position.

František Žiačik
Furthermore, and not withstanding the possible irregularities introduced in the building of this particular database instance, peoples' names can be irregular enough to make parsing the surname a non trivial task (ex: mulitiple tokens with "Di Angelo", very short surnames "Le", "Zef" etc.)
mjv
Thanks for link. It is interesting.
den bardadym
@mjv. I hava a strict format of name field ("Sername Name").
den bardadym
I accept your answer. Because other answer not for my question. Thanks.
den bardadym
+1  A: 

Better yet, make a schema that doesn't have the surenames and first names mixed in the same column:

doctors ( id int, firstname varchar(255), lastname varchar(255) )

Then you don't need those (slow) string operations.

inflagranti
Yes i know it. But i work with created db.
den bardadym