Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a value name during output

Tags:

sql

I am trying to change the name of a stored value when I execute my SQL script:

SELECT
    PERSONNUM, 
    PAYCODENAME, 
    CAST(WFCTIMEINSECONDS AS FLOAT)/3600 AS Total_Hours
FROM
    VP_ALLTOTALS
WHERE
    Applydate >= '09/25/2011' AND
    Applydate <= '10/01/2011' AND
    PAYCODENAME IN ('Vacation'
                    ,'Sick Leave - Paid'
                    ,'Personal Business - Paid'
                    ,'Comp Time - Paid'
                     )

I want the Vacation to be VAC, Sick Leave - Paid to be SIC, Personal Business - Paid to be PER and

like image 822
Kenh426 Avatar asked Oct 07 '11 19:10

Kenh426


People also ask

How do you rename a value in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

How do you replace a value in a select statement?

The Replace statement is used to replace all occurrences of a specified string value with another string value. The Replace statement inserts or replaces values in a table. Use the Replace statement to insert new rows in a table and/or replace existing rows in a table.

Can we rename a column in the output of SQL query?

SQL Server permits you to change the column whenever required. You need to rename column in SQL where the column name is meaningless or doesn't meet its purpose.

How do you replace a word in a table in SQL?

SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.


1 Answers

The easiest way is a list of CASE options for substitutions.

SELECT PERSONNUM
       PAYCODENAME CASE WHEN 'Vacation' THEN 'VAC'
                        WHEN 'Sick Leave - Paid' THEN 'SIC'
                        WHEN 'Personal Business - Paid' THEN 'PER'
                        ELSE PAYCODENAME END AS PAYCODENAME
       ....
like image 137
JNK Avatar answered Sep 24 '22 15:09

JNK