Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Query

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2012-08-27 : 14:36:25
Hi All,

I have one table called emp_master i have one column emp_name,fname,lname.

Now i have to update fname and lname based on Emp_Name.
Please suggest some query

e.g
Sanjay Test so FNAme should update Sanjay and Test as LNAME
Test1 Test so FNAme should update Test1 and Test as Test

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-08-27 : 15:24:24
There's no sure-fire way to do this, unless FNAME has only 2 parts to it. If someone has a middle name or initial, or any deviation from just first and last name, you'll never be able to programatically address all situations. That being said
declare @empname varchar(20)
set @empname = 'Sanjay Test'

select substring(@empname,1,charindex(' ',@empname)-1) as FNAME
,substring(@empname,charindex(' ',@empname)+1,50) as LNAME

or

select parsename(replace(@empname,' ','.'),1) as LNAME
,parsename(replace(@empname,' ','.'),2) as FNAME

Jim




Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-27 : 17:42:10
first analysis various formats in which you've name values coming and then you might have to apply separate update logic for each of them

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -