I have a column named Employee name in table 1
Example: Mr.FirstName LastName
and so on with various titles but there are employee names without the title in the same column.I am about to split the single column and do an insert in the new table (table 2) with three different columns like FirstName, LastName and Title.So while doing an insert into the new table I am not able to split the employee column name like I mentioned.Any help will be really appreciated I started with LINQ so I am not aware of much SQL functions.
Update : Sample data
Here's one example.
DECLARE #name varchar(100) = 'Mr.FirstName LastName'
SELECT
LEFT(#name, CHARINDEX('.', #name)) AS Title,
SUBSTRING(#name, CHARINDEX('.', #name)+1, CHARINDEX(' ', #name)-CHARINDEX('.', #name)) AS FirstName,
SUBSTRING(#name, CHARINDEX(' ', #name)+1, 1000) AS LastName
It takes...
the left part till the . as Title.
everything after the . until the first space as FirstName
everything after the first space as LastName
Note: There's no check for errors, if the name does not fit into this pattern.
For the simplest case you specified in the question the following query should work
SELECT *,
SUBSTRING(Employee_name, 0, CHARINDEX('.', Employee_name)) AS Title,
SUBSTRING(Employee_name,
CHARINDEX('.', Employee_name)+1,
CHARINDEX(' ', Employee_name)) AS FirstName,
SUBSTRING(Employee_name,
CHARINDEX(' ', Employee_name)+1,
LEN(Employee_name)) AS LastName
FROM Employee;
I have query that looks something like the following:
select firstname, lastname
from names
where firstname = 'john'
I would like to have something like the following (conceptually):
select
[names.firstname + names.lastname] as 'fullname'
from names
where names.firstname = 'john'
Of course, this returns an invalid column name 'names.firstname + names.lastname'.
Is it possible to return a single aliased column containing the result of two columns from the same table?
Thanks for the comments; In this case it looks like the following will work as needed:
CONCAT(ISNULL(names.FirstName, '(No First Name)',
' ', ISNULL(names.LastName, '(No Last Name)')) AS 'FullName'
I am following sample data in SQL Table:-
First_Name Last_Name Concatenation(F_Name,L_Name)
-------------------------------------------------------------
Mohd Nazir Mohd,Nazir
I want to see the result as
Full_Name = Mohd Nazir
within the same table
How about
ALTER TABLE dbo.YourTableNameHere
ADD FullName AS ISNULL(First_Name, '') + ' ' + ISNULL(Last_Name, '')
With this computed column, you have a new column in your table that'll always contain the concatenation of first and last name, separated by a space
I have a table USERS and it has a column FULLNAME that contains full users names in the format FirstName LastName and I need to switch the data, and update it in the same column, using this format LastName, FirstName.
Ex. James Brown needs to be switched to Brown, James and updated in the same column (FULLNAME)
Is there any way to do it?
Thanks.
The best solution would be to split the elements into two columns (LastName, FirstName). However, if you want to try and squeeze it into 1 column, and you're assuming that every name is split by a single space
DECLARE #Users TABLE ( Name VARCHAR(100) )
INSERT INTO #Users
( Name
)
SELECT 'James Brown'
UNION ALL
SELECT 'Mary Ann Watson'
SELECT RIGHT(Name, CHARINDEX(' ', REVERSE(Name)) - 1) + ', ' + LEFT(Name,
LEN(Name)
- CHARINDEX(' ',
REVERSE(Name)))
FROM #Users
This also assumes that if they have more than two names sperated by a space, then the last word is the last name, and everything else is first name. Works for "Mary Ann Watson", but not "George Tucker Jones", if "Tucker Jones" is a last name.
Assuming they only have 1 space in the name:
UPDATE USERS
SET FULLNAME = RIGHT(FULLNAME,len(FULLNAME) - CHARINDEX(' ',FULLNAME)) +', '+ LEFT(FULLNAME,charindex(' ',FULLNAME)-1)
WHERE LEN(FULLNAME) - LEN(REPLACE(FULLNAME, ' ', '')) = 1
I have a employee table and the sample data in it is like this. I am using sql server 2008.
CREATE TABLE employee (name nvarchar(255))
insert into employee (name) values ('Alex,AlexMartin'),
('John,John'),
('Mayr,Mayr'),
('Shel,Sheila'),
('corolla,corolla,corolla3'),
('Mary4,Mary,Mary'),
('Justin,Justin,Justin'),
('Sara,Sara,Sara,Sara'),
('clarence,clarence,clarence458,clarence,clarence'),
('fiesta,fiesta,fiesta,fiesta,fiesta'),
('scorpio1,scorpio,scorpio,scorpio4,scorpio')
I want to delete a value if all the values in the string are same example: John,John should be replaced by 'John'. If all the names in string are not equal like Shel,Sheila it should retain both the values.
For this I am using
update employee set name=(select PARSENAME(REPLACE(name, ',', '.'), 2)) where (select PARSENAME(REPLACE(name, ',', '.'), 2))
like (select PARSENAME(REPLACE(name, ',', '.'), 1)) but it is changing Mary4,Mary,Mary to Mary. I tried combinations for 5, 4, and 3 names but there is no use. In fact for five names this code is not at all working. Is there any efficient way to do this?
This will get the data in the format you're looking for:
CREATE TABLE #Employee (Name NVarChar(255));
INSERT INTO #Employee (Name) VALUES ('Alex,AlexMartin'),
('John,John'),
('Mayr,Mayr'),
('Shel,Sheila'),
('corolla,corolla,corolla3'),
('Mary4,Mary,Mary'),
('Justin,Justin,Justin'),
('Sara,Sara,Sara,Sara'),
('clarence,clarence,clarence458,clarence,clarence'),
('fiesta,fiesta,fiesta,fiesta,fiesta'),
('scorpio1,scorpio,scorpio,scorpio4,scorpio'),
('Another');
SELECT Name, CASE
WHEN CHARINDEX(Name, ',') = 0 THEN Name
WHEN
REPLACE(REPLACE(Name, LEFT(Name, CHARINDEX(',', Name) - 1), ''), ',', '') = ''
THEN LEFT(Name, CHARINDEX(',', Name) - 1)
ELSE Name
END Result
FROM #Employee;
DROP TABLE #Employee;
It works by getting the first name in the comma-separated list. By your requirements all of the items have to be identical in order to condense the list, so it doesn't matter which element in the list we use for comparison.
All occurrences of the first item are removed from the list. Then all of the commas are removed. If the resulting value is empty (i.e. '') then we know all of the items are identical. In that case the first element is used as the Result value. Otherwise, we return the original list unchanged.
EDIT: Some of your data mustn't have a , in it, so I've updated the answer to take care of that. It will just return the same input if a delimiter doesn't exist.