sql server - Adding extracted columns to a table - SQL -
in database table actors, have 2 feilds: name , salary. 'name' consists of last , first names separated comma , space. trying add 2 more columns: fname , lname, in addition existing columns, , able output these 2 new columns, struggling add these columns actors table.
name / salary -> name / salary / fname / lname
can please me , let me know how this? thank in advance.
here sql commands wrote:
select case when name '%,%' substring(name, charindex(',', name)+2, len(name)) else substring(name, 1, len(name)) end fname, case when name '%,%' substring(name, 1, charindex(',', name)-1) end lname actors;
don't add new columns table, if these going derived existing column. instead, add computed columns:
alter table actors add fname (case when name '%,%' substring(name, charindex(',', name)+2, len(name)) else name end); alter table actors add lname (case when name '%,%' left(name, charindex(',', name) - 1) end);
the values computed automatically when query table.
Comments
Post a Comment