Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change column name and change a value in a table via a stored procedure?

I have the table below :

Desk_name | head
ali       | john 
cars ln   | max
pol fr    | max
jus       | john

I want to create a stored procedure in order to have in return :

Name      | sector_head
ali mm    | john
cars ln   | max
pol fr    | max
jus mm    | john

As you can see the column name has changed (from 'Name' to 'Desk_name') and every name in the 'Name' column has the abbreviation "mm" at the end of each values if it is a single word (ali and jus).

How can I do such a stored procedure (i am using sql server)?

Thanks

like image 539
francops henri Avatar asked Feb 18 '23 23:02

francops henri


1 Answers

You need to supply ALIAS on it, example

SELECT CASE WHEN LTRIM(RTRIM(Desk_name)) LIKE '% %'  -- finds any space in string
            THEN Desk_name 
            ELSE Desk_name + ' mm'
       END AS Name, 
       head AS sector_head 
FROM   tableName

SQLFiddle Demo

like image 91
John Woo Avatar answered Feb 25 '23 11:02

John Woo