How to get the First name,second name,Third name ?

I have a column in a table with names.Now i want to select that names like first name,second and third.

I/p like this----- names
Chandu
Kannaka prasad
Rama krishna
Kiran kumar Kora
Balaji
Chandra sekhar potta
O/p I want------ Firstname Secondname Thirdname
chandu
kanaka Prasad
Rama Krishna
Kiran Kumar Kora
Balaji
Chandra Sekhar Potta

Questions by chandra.pc

Showing Answers 1 - 23 of 23 Answers

rabbi08

  • Jun 6th, 2008
 

select firstname||' '||secondname||' '||thirdname from tablename;

Here "||" is the concatenation operatot so names and space is concatenated.

  Was this answer useful?  Yes

vmdsr

  • Jun 10th, 2008
 

A simple select of first name, second name, third name should display in that order since some of the rows have second and third name as null.

  Was this answer useful?  Yes

SELECT SUBSTR('first second third',0,INSTR('first second third',' ', 1, 1))
FROM dual;
output: 'first         '


SELECT SUBSTR('first second third',
              INSTR('first second third',' ', 1, 1)+1,
              INSTR('first second third',' ',1,2)-INSTR('first second third',' ',1,1)-1)
FROM dual;
output: ' Second'

SELECT SUBSTR('first second third',
              INSTR('first second third',' ', 1, 2)+1,
              INSTR('first second third',' ',1,2)-INSTR('first second third',' ',1,1)-1)
FROM dual;
Output:'third     '

Combine above 3 queries into a select statement as follows to get desired output of 3 names in 3 seperate columns
Select substr query1, substr query2, substr query3 from dual;

Thanks
Neo

farhanbaig

  • Jun 6th, 2011
 

I am guessing you are looking for initcap function which are as follows:

select initcap(first_name||' '||second_name||' '||third_name) from employees;

  Was this answer useful?  Yes

Code
  1. DECLARE @Name NVARCHAR(MAX), @FName NVARCHAR(MAX), @MName NVARCHAR(MAX), @LName NVARCHAR(MAX)

  2. SELECT @Name = SAURABH KANT CHAKRAVARTI

  3. --select @Name = SAURABH KANT

  4.  

  5.  

  6.  

  7. SELECT @FName=SUBSTRING (@Name, 1, (PATINDEX (% %, RTRIM (LTRIM(@Name) ))))

  8. SET        @Name = SUBSTRING (@Name, LEN(@FName)+1, LEN (@Name) - LEN(@FName))

  9. SELECT @MName = SUBSTRING (@Name, 1 , (PATINDEX (% %, RTRIM (LTRIM(@Name) ))))

  10. SELECT @LName = LTRIM (RTRIM (SUBSTRING (@Name, LEN(@MName)+1, LEN (@Name) - LEN(@MName))))

  11.  

  12. SELECT @FName AS Fname

  13. SELECT @MName AS Mname

  14. SELECT @LName AS Lname

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.