Solved

help with insert if statement

Posted on 2013-06-07
2
325 Views
Last Modified: 2013-06-07
Need to copy data from FirstMiddle field to Salutation field if FirstMiddle is not empty and Salutation is empty. I've been trying this:
INSERT INTO [Shelby].[NANames] ([Salutation])
VALUES [Shelby].[NANames] ([FirstMiddle]);
 WHERE (([Shelby].[NANames].[FirstMiddle] <> '')  AND ([Shelby].[NANames].[Salutation] = '') )

Open in new window



But I'm getting a syntax error, can anyone help?
0
Comment
Question by:garysamuels
2 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 39229813
Try this (after backing up):

 UPDATE Shelby.NANames
 SET Salutation = FirstMiddle WHERE Salutation = '' AND FirstMiddle <> ''

Open in new window


It's a good idea to try a select first, to make sure you'll be updating the right data:

 SELECT * FROM Shelby.NANames
WHERE Salutation = '' AND FirstMiddle <> ''

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39229820
INSERT INTO creates a new row - not want you want to do, I assume. Sounds like you want to update the Salutation field:
UPDATE Shelby.NANames
SET Salutation = FirstMiddle
WHERE FirstMiddle <> '' and (Salutation = '' or Salutation is null)

Open in new window

Note that empty string is not the same as NULL, so I have checked for both.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now