Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
eid ename
1   kula sekhar reddy
2   sachin ramesh tendulker
3   mahindra singh dhoni
4   ravi chandra aswin


Required Output like bellow

eid   firstname  middlename   lastname
1      kula       sekhar       reddy
2      sachin     ramesh       tendulker
3       mahindra  singh        dhoni
4       ravi      chandra     aswin


What I have tried:

Please give Query to display names like above
Posted
Updated 2-Jul-17 20:09pm
v2
Comments
Afzaal Ahmad Zeeshan 2-Jul-17 6:54am    
We have no idea about the table, schema, or anything. How can we provide a query for this?
PIEBALDconsult 2-Jul-17 11:44am    
To add to what has already been said (avoid string manipulation in SQL), when necessary, you will find better performance by writing a table-valued function in C#.
https://www.codeproject.com/Articles/37377/SQL-Server-CLR-Functions
https://docs.microsoft.com/en-us/sql/relational-databases/clr-integration-database-objects-user-defined-functions/clr-table-valued-functions
https://technet.microsoft.com/en-us/library/ms131103(v=sql.110).aspx
F-ES Sitecore 3-Jul-17 4:39am    
It is important that your database properly models your data. If first, middle and last names are recognisable entities in your system then they should be stored as such. So store each section in its own field and if you want to show them all together concatenate the results in your code. You'll find doing it that way is far, easier than having a single field that you split in SQL

 
Share this answer
 
It's a bad idea: it's much better to handle it at the data entry point by splitting it then and using three columns to hold the data instead of one (concatenation is trivial, but SQL's string handling is ... um ... primitive at best.

But it can vbe done: sql server - SQL: parse the first, middle and last name from a fullname field - Stack Overflow[^] - it's not pretty, it's not easy to change: but it works.

See what I mean about it being the wrong way to handle it?
 
Share this answer
 
It is bad idea to store from the beginning name in one field (if you need it separated), as SQL is a but weak in string manipulation...
Until SQL 2016 there is no built in split function at all...
however it is possible (but better to re-normalize your data)...
In 2016: STRING_SPLIT (Transact-SQL) | Microsoft Docs[^]
Pre 2016 (works just like the 2016 version):
SQL
CREATE FUNCTION STRING_SPLIT (@STR NVARCHAR(MAX), @DEL NCHAR(1) = ' ')
RETURNS
	@RV TABLE (VALUE NVARCHAR (MAX))
AS
BEGIN

 DECLARE @PART NVARCHAR(MAX)
 DECLARE @POS INT

 WHILE CHARINDEX(@DEL, @STR) > 0
 BEGIN
  SELECT @POS  = CHARINDEX(@DEL, @STR)  
  SELECT @PART = SUBSTRING(@STR, 1, @POS - 1)

  INSERT INTO @RV 
  SELECT @PART

  SELECT @STR = SUBSTRING(@STR, @POS + 1, LEN(@STR) - @POS)
 END

 INSERT INTO @RV
 SELECT @STR

 RETURN
END
 
Share this answer
 
Another way to achieve that is to use CTE (Common Table Expressions) with Pivot[^]:

SQL
DECLARE @tmp TABLE(eid INT IDENTITY(1,1), ename VARCHAR(30))

INSERT INTO @tmp (ename)
VALUES('kula sekhar reddy'),
('sachin ramesh tendulker'),
('mahindra singh dhoni'),
('ravi chandra aswin')


;WITH CTE AS 
(
	--init query
	SELECT 1 AS MyCounter, eid, LEFT(ename, CHARINDEX(' ', ename)-1) AS PartOfName, RIGHT(ename, LEN(ename) - CHARINDEX(' ', ename)) AS Remainder
	FROM @tmp 
	WHERE CHARINDEX(' ', ename)>0
	UNION ALL
	--loop
	SELECT MyCounter + 1 AS MyCounter, eid, LEFT(Remainder, CHARINDEX(' ', Remainder)-1) AS PartOfName, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(' ', Remainder)) AS Remainder
	FROM CTE 
	WHERE CHARINDEX(' ', Remainder)>0
	UNION ALL
	--final
	SELECT MyCounter + 1 AS MyCounter, eid, Remainder AS PartOfName , NULL AS Remainder
	FROM CTE 
	WHERE CHARINDEX(' ', Remainder)=0
) 
SELECT eid, [1] AS FirstName, [2] As SecondName, [3] AS LastName
FROM (
	SELECT eid, MyCounter, PartOfName
	FROM CTE
	) AS dt 
PIVOT(MAX(PartOfName) FOR MyCounter IN ([1], [2], [3])) AS pvt


Result:
eid	FirstName	SecondName	LastName
1	kula		sekhar		reddy
2	sachin		ramesh		tendulker
3	mahindra	singh		dhoni
4	ravi		chandra		aswin


For further details, please see:
Using Common Table Expressions[^]
Recursive Queries Using Common Table Expressions[^]
WITH common_table_expression (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900