SQL: parse the first, middle and last name from a fullname field

后端 未结 23 1626
粉色の甜心
粉色の甜心 2020-11-27 10:47

How do I parse the first, middle, and last name out of a fullname field with SQL?

I need to try to match up on names that are not a direct match on full name. I\'d

23条回答
  •  孤城傲影
    2020-11-27 11:21

    I'm not sure about SQL server, but in postgres you could do something like this:

    SELECT 
      SUBSTRING(fullname, '(\\w+)') as firstname,
      SUBSTRING(fullname, '\\w+\\s(\\w+)\\s\\w+') as middle,
      COALESCE(SUBSTRING(fullname, '\\w+\\s\\w+\\s(\\w+)'), SUBSTRING(fullname, '\\w+\\s(\\w+)')) as lastname
    FROM 
    public.person
    

    The regex expressions could probably be a bit more concise; but you get the point. This does by the way not work for persons having two double names (in the Netherlands we have this a lot 'Jan van der Ploeg') so I'd be very careful with the results.

提交回复
热议问题