Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help with insert if statement

Posted on 2013-06-07
2
Medium Priority
?
346 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:Gary Samuels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 2000 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 71

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

618 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