SQL How to replace values of select return?

前端 未结 7 1169
无人共我
无人共我 2020-12-04 18:02

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

But in S

相关标签:
7条回答
  • 2020-12-04 18:13

    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
      
    0 讨论(0)
  • 2020-12-04 18:19

    You can use casting in the select clause like:

    SELECT id, name, CAST(hide AS BOOLEAN) FROM table_name;
    
    0 讨论(0)
  • 2020-12-04 18:27

    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.

    0 讨论(0)
  • 2020-12-04 18:27

    I saying that the case statement is wrong but this can be a good solution instead. If you choose to use the CASE statement, you have to make sure that at least one of the CASE condition is matched. Otherwise, you need to define an error handler to catch the error. Recall that you don’t have to do this with the IF statement.

    SELECT if(hide = 0,FALSE,TRUE) col FROM tbl; #for BOOLEAN Value return
    

    or

    SELECT if(hide = 0,'FALSE','TRUE') col FROM tbl; #for string Value return
    
    0 讨论(0)
  • 2020-12-04 18:31

    You can do something like this:

    SELECT id,name, REPLACE(REPLACE(hide,0,"false"),1,"true") AS hide FROM your-table
    

    Hope this can help you.

    0 讨论(0)
  • 2020-12-04 18:34

    Replace the value in select statement itself...

    (CASE WHEN Mobile LIKE '966%' THEN (select REPLACE(CAST(Mobile AS nvarchar(MAX)),'966','0')) ELSE Mobile END)

    0 讨论(0)
提交回复
热议问题