Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want my name, i.e., "Anaya Upadhyay" to view into two columns using a select statement.
One column should have my firstname, another the lastname.

Use the following statement:
select 'Anaya Upadhyay' as [name]

Any ideas???
Posted
Comments
walterhevedeich 23-May-13 23:23pm    
Unclear
Anaya Upadhyay 24-May-13 3:51am    
i want to split my name in two columns or rows.

SQL
declare @name  nvarchar(50)
set @name = 'Walter Hevedeich'

SELECT CASE
         WHEN @name LIKE '% %' THEN LEFT(@name, Charindex(' ', @name) - 1)
         ELSE @name
       END As FirstName,
       CASE
         WHEN @name LIKE '% %' THEN RIGHT(@name, Charindex(' ', Reverse(@name)) - 1)
       END As LastName



check this one..
 
Share this answer
 
Given that your name column contains this format

[Firstname][space][LastName]

You can use CHARINDEX and SUBSTRING to split it into 2 columns. The following is an example

SQL
DECLARE @name AS VARCHAR(30)
DECLARE @spaceIndex AS INT
SET @name = 'Walter Hevedeich'
SET @spaceIndex = CHARINDEX(' ', @name)
SELECT SUBSTRING(@name, 1, @spaceIndex) AS FirstName,
SUBSTRING(@name,@spaceIndex,LEN(@name)) AS LastName


Also, check this[^] link for the complete string functions for SQL.
 
Share this answer
 
Comments
Anaya Upadhyay 24-May-13 4:37am    
Thanks for the solution.
SQL
SELECT FirstName + '   ' + LastName  'Column_Name' FROM [Table_Name]



TRY LIKE THIS
 
Share this answer
 
v3
Comments
Arun Vasu 23-May-13 23:24pm    
IF YOU ARE SATISFIED WITH MY ANSWER THEN MARK AS ANSWER
Anaya Upadhyay 24-May-13 3:51am    
This is not what i meant, i want to split a single text block i.e. my name into two individual columns or rows.

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