Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL How to replace values of select return?

Tags:

sql

select

mysql

In my database (MySQL) table, has a column with 1 and 0 for represent true and false respectively.

But in SELECT, I need it replace for true or false for printing in a GridView.

How to I make my SELECT query to do this?

In my current table:

 id   |  name    |  hide   1   |  Paul    |  1   2   |  John    |  0   3   |  Jessica |  1 

I need it show thereby:

  id  |  name    |  hide   1   |  Paul    |  true   2   |  John    |  false   3   |  Jessica |  true 
like image 348
Lai32290 Avatar asked May 25 '13 19:05

Lai32290


People also ask

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.

What does replace () do in SQL?

The REPLACE() function replaces all occurrences of a substring within a string, with a new substring.

How do you replace multiple values in SQL?

SELECT REPLACE(REPLACE(REPLACE(REPLACE('3*[4+5]/{6-8}', '[', '('), ']', ')'), '{', '('), '}', ')'); We can see that the REPLACE function is nested and it is called multiple times to replace the corresponding string as per the defined positional values within the SQL REPLACE function.

How do I swap values in SQL?

SET Col1 = Col2, Col2 = Col1; When you run above update statement, the values of the columns will be swapped in SQL Server. There is no need for temporary column, variable or storage location in SQL Server.


2 Answers

You have a number of choices:

  1. Join with a domain table with TRUE, FALSE Boolean value.
  2. Use (as pointed in this answer)

    SELECT CASE WHEN hide = 0 THEN FALSE ELSE TRUE END FROM 

    Or if Boolean is not supported:

    SELECT CASE WHEN hide = 0 THEN 'false' ELSE 'true' END FROM 
like image 106
frugal-one Avatar answered Oct 12 '22 12:10

frugal-one


I got the solution

   SELECT     CASE status       WHEN 'VS' THEN 'validated by subsidiary'       WHEN 'NA' THEN 'not acceptable'       WHEN 'D'  THEN 'delisted'       ELSE 'validated'    END AS STATUS    FROM SUPP_STATUS 

This is using the CASE This is another to manipulate the selected value for more that two options.

like image 26
Sandeep Kamath Avatar answered Oct 12 '22 14:10

Sandeep Kamath