Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using if statement how to replace values

I have two tables,

Database 1:

Table 1:

  • Id -001(int)

  • Name -xxx(varchar)

  • Male -0 (bit)

  • Female-1 (bit)

Database 2:

Table 2:

  • Id -1 (int)

  • Ca.Name-yyy (varchar)

  • Gender - M or F (varchar)

These are my tables. I want to update data from database2, Table 2 to database1, table1. So i create update query. But I have problem with Gender field

Select 'UPDATE T1
 SET T1.MALE ='+ CASE WHEN r.Gender = 'M' THEN 0 ELSE 1 END

 FROM T2 As r

In the below code, what I starred I have a doubt how to use if statement here. Any one understand rectify my problem?

like image 804
Duk Avatar asked Apr 13 '13 05:04

Duk


People also ask

How do you change the values in a column based on condition in r?

Replace column values based on checking logical conditions in R DataFrame is pretty straightforward. All you need to do is select the column vector you wanted to update and use the condition within [] . The following example demonstrates how to update DataFrame column values by checking conditions on a numeric column.

How do I replace specific values in R?

To replace a column value in R use square bracket notation df[] , By using this you can update values on a single column or on all columns. To refer to a single column use df$column_name .


1 Answers

Use case when for this purpose.

Follow this question :

SQL Server: CASE WHEN OR THEN ELSE END => the OR is not supported

CASE  
  WHEN GENDER='M' THEN 0 
  ELSE 1 
END 

This can be in your case.

For more syntax details: CASE (Transact-SQL)

like image 128
Freelancer Avatar answered Oct 05 '22 07:10

Freelancer