Oracle SQL Developer - SUBSTR and LIKE [duplicate] - sql

This question already has answers here:
ORACLE:- SELECT First Names , by removing space after the first word
(1 answer)
How to check if the length of a string is more than one word and keep only the first word else keep the entire string in SQL?
(2 answers)
Closed last year.
I have a column NAME_SURNAME (made of names and surnames) from a table people:
John Smith
Tim Burton
Harry Potter
I need to write a query to obtain only the names:
John
Tim
Harry
I am trying in this way nad it doesn't work:
select NAME_SURNAME, substr(NAME_SURNAME, 1, LIKE '% %')
from PEOPLE
I don't figure out how to use LIKE in this exercise.
Thank you really much

Use INSTR with SUBSTR to take a substring up to the first space:
SELECT NAME_SURNAME, SUBSTR(NAME_SURNAME, 1, INSTR(NAME_SURNAME, ' ') - 1)
FROM PEOPLE;
Demo
For a regex approach, we can use REGEXP_SUBSTR:
SELECT NAME_SURNAME, REGEXP_SUBSTR(NAME_SURNAME, '^\S+') AS FIRST_NAME
FROM PEOPLE;

Related

Splitting Full Name into First Name and Last Name : (Redshift)

I have a table below with names of the customer
fullname
Ash Ketchum
Mary Joe Keth
John
I want to split it into first and last names
firstname lastname
Ash Ketchum
Mary Joe Keth
John
I referred to this post and tried all the solution but nothing seems to work for me. I already did the same exercise in SQLServer using CHARINDEX() but seems like it's not supported in redshift
, same is the case with SUBSTR()
Simplest way to split human name in postgres?
You should be able to use SPLIT_PART():
SPLIT_PART(fullname, ' ', 1) as first_name,
SPLIT_PART(fullname, ' ', 2) as last_name

How to parse a row in SQL into columns (separator-spaces)

I need to parse row (with spaces) into two different columns in SQL. The rows look like that:
FullName
John Mayer
Rajon Jennings
Donna Fraye
Michael Moore
So the expected result is :
Name
John
Rajon
Donna
Michael
Surname
Mayer
Jennings
Fraye
Moore
How can i do that in SQL?
If you have a requirement like First Name should be string before first space and rest everything what follows should go as Last name , you can update two columns as:
Update T
Set T.FirstName = Left(ltrim(rtrim(FullName)), CHARINDEX(' ',ltrim(rtrim(FullName))))
,T.LastName = Right(ltrim(rtrim(FullName)),len(FullName)- CHARINDEX(' ',ltrim(rtrim(FullName))))
From Test T
Check Demo here..
You can use a split function.This function must be created.
Here are some examples :
http://ole.michelsen.dk/blog/split-string-to-table-using-transact-sql/
Split function by comma in SQL Server 2008
If there is only ever 1 space / 2 name and you don't have to cater for names like Vincent van Gough, then a combination of substring and charindex should do.
select substring(fullname, 1, charindex(' ', fullname)) as name,
substring(fullname, charindex(' ', fullname)+1, len(fullname)-charindex(' ', fullname)) as surname
from tbl
This does not cover people with the same first and last name (ie. john john) but will work for rest. Not recommended but is a different solution.
select left(fullname, charindex(' ',fullname)) Name,
replace(fullname,left(fullname, charindex(' ',fullname)),'') Surname
from names
Fiddle

Split data from one colunn into two new columns in SQL on multiple rows [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 8 years ago.
I'm using SQL Express 2012, and I'm rather new to it so this website has been a blessing so far! I'm now stuck on a query that I've not found a suitable answer to.
I have a table called Claims Passed. In this I have a column called Client_Name, in this is a list of names, these contain first and second names split by a space (e.g John Smith). I've created two new columns, Client_First_Name and Client_Surname.
What I'm trying to do is get the get the first name in to first name column and the surname into the surname column.
I came across something like this but it was only for one row, not all the rows in one go.
How can i do this?
This is the basic syntax you need, assuming each name has only one first name:
UPDATE [Claims Passed]
SET Client_First_Name = SUBSTRING(Client_Name, 1, CHARINDEX(' ', Client_Name) - 1),
Client_Surname = SUBSTRING(Client_Name,CHARINDEX(' ', Client_Name) + 1, LEN(Client_Name)
The problem on your approach is when a client has more than one surname and one last name this won't work. You could try something like this:
INSERT INTO Client_First_Name VALUES(SELECT Client_Name LIKE '% %' THEN LEFT(Client_Name, Charindex(' ', Client_Name) - 1));
INSERT INTO Client_Surname VALUES(SELECT Client_Name LIKE '% %' THEN RIGHT(Client_Name, Charindex(' ', Reverse(Client_Name)) - 1));

Combining Columns on Blank Rows

I have a question about the possibility of combining columns based on blanks ('') in a row. The reason behind this is because I am using a charindex to strip off the middle initial in the first name. However, if the data does not contain a middle initial I have it set to return a blank ('').
Here is an example of what I am looking to do.
First_Name First_Name2 Column_Needed
John B John John
Fred Fred
Mary D Mary Mary
Mike Mike
Scott S Scott Scott
I would like to have a third column that would combine the two columns as one column with no blanks and no middle initials but all first names.
Some example of the code I am using to strip middle initial...
LEFT([First_Name], CHARINDEX(' ', [First_Name])) AS [First_Name2]
You need to use a CASE statement in SQL:
SELECT CASE LEFT([First_Name], CHARINDEX(' ', [First_Name])) WHEN '' THEN [First_Name] ELSE LEFT([First_Name], CHARINDEX(' ', [First_Name])) END as Column_Needed
#Joseph B's is a much better answer.
You can use the COALESCE function to get the First Name without the middle initial, as below:
SELECT COALESCE([First_Name2], [First_Name]) as Column_Needed

Postgres: order data by part of string

I have a column name that represents a person's name in the following format:
firstname [middlename] lastname [, Sr.|Jr.]
For, example:
John Smith
John J. Smith
John J. Smith, Sr.
How can I order items by lastname?
A correct and faster version could look like this:
SELECT *
FROM tbl
ORDER BY substring(name, '([^[:space:]]+)(?:,|$)')
Or:
ORDER BY substring(name, E'([^\\s]+)(?:,|$)')
Or even:
ORDER BY substring(name, E'([^\\s]+)(,|$)')
Explain
[^[:space:]]+ .. first (and longest) string consisting of one or more non-whitespace characters.
(,|$) .. terminated by a comma or the end of the string.
The last two examples use escape-string syntax and the class-shorthand \s instead of the long form [[:space:]] (which loses the outer level of brackets when inside a character class).
We don't actually have to use non-capturing parenthesis (?:) after the part we want to extract, because (quoting the manual):
.. if the pattern contains any parentheses, the portion of the text that
matched the first parenthesized subexpression (the one whose left
parenthesis comes first) is returned.
Test
SELECT substring(name, '([^[:space:]]+)(?:,|$)')
FROM (VALUES
('John Smith')
,('John J. Smith')
,('John J. Smith, Sr.')
,('foo bar Smith, Jr.')
) x(name)
SELECT *
FROM t
ORDER BY substring(name, E'^.*\\s([^\\s]+)(?=,|$)') ASC
While this should provide the sorting you are looking for, it would be a lot cheaper to store the name in multiple columns and index them based on which parts of the name you need to sort by.
You should use functional index for this purpose
http://www.postgresql.org/docs/7.3/static/indexes-functional.html
In your case somehow....
CREATE INDEX test1_lastname_col1_idx ON test1 (split_part(col1, ' ', 3));
SELECT * FROM test1 ORDER BY split_part(col1, ' ', 3);