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.
How Do you write if then statements in SQL ?
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
2 answers
Sort by: Most helpful
-
-
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 tableAccording 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