How Do you write if then statements in SQL ?

Debilon 431 Reputation points
2022-03-12T16:54:02.567+00:00

Do you write if then statements in SQL ?

I have a contact table that contains FirstName, LastName, Address, Phone1, Phone2,Email1 and Email2
I have an Owner table that contains AccountNumber, FirstName, LastName, Phone1, Phone2,Email1 and Email2

What i would like to do is check if phone1 and email1 from the contact table exist for a specific owner (FirstName, LastName and Address are the same)
and if so enter the additional Phone2 and Email2 from the contact table to the owner table

in VB / VC i can write an IF - Then statement that if phone1 exist and

FirstName & LastName are the same then enter phone2 into its column

But how do you do so in SQL ?

Developer technologies Transact-SQL
{count} votes

2 answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,431 Reputation points
    2022-03-13T04:09:01.297+00:00

    As Erland said, your question is not clear. In general there is CASE expression you can use, IIF function you can use. To check if the column contains data or not you may want to use ISNULL(columnName, '') = '' -- this means that column is either NULL or empty.

    0 comments No comments

  2. LiHong-MSFT 10,056 Reputation points
    2022-03-14T06:47:29.153+00:00

    Hi @Debilon

    check if phone1 and email1 from the contact table exist for a specific owner (FirstName, LastName and Address are the same)
    and if so enter the additional Phone2 and Email2 from the contact table to the owner table

    According to your description, I can only tell that you may need to use IF Exists, Case When, UPDATE......
    Could you please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …) along with your expected result? So that we’ll get a right direction and make some test.

    Best regards,
    LiHong

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.