Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Statement to populate new field

Author  Topic 

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-06 : 14:42:50
I have a SQL database that I have recently added a new field. To populate the field I need to create a statement to



for a [Location Code] = "HOI" and [Category] <> "Monitor" then [pcname]will be "C" = [Asset tag]

I have about 10 such statements so IIF won't work. I'm new to SQL.

Can someone please provide some help with the syntax

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-05-06 : 14:49:53
Your question is not fully clear, but maybe something like this?

update table
set [Asset tag]= case
when [Location Code] = 'HOI' and [Category] <> 'Monitor' then [pcname]
when ... then ...
else ...
end



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-06 : 14:53:30
sorry

for a [Location Code] = "HOI" and [Category] <> "Monitor" then [pcname]will be "C" + [Asset tag]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-05-06 : 15:03:03
[code]
update table
set [pcname]= case
when [Location Code] = 'HOI' and [Category] <> 'Monitor' then 'C'+[Asset tag] -- if [Asset tag] is numeric: use 'C' + convert(varchar(255),[Asset tag])
when ... then ...
else ...
end

[/code]
I don't know much about [Asset tag]


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-06 : 15:11:56
Thanks for your help. I am getting this error

"Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'table'.
Go to Top of Page

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-06 : 15:16:44
OK, I got it. forgot to replace table with table name
Go to Top of Page

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-07 : 09:27:27
How can I set it to clear all info in the PCname field first?
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-05-07 : 10:36:42
Before you blow up your database, follow the links in my signature. They have VERY good advice in them.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

tbarkdull
Starting Member

6 Posts

Posted - 2009-05-07 : 10:52:17
Thanks DonAtWork
Go to Top of Page
   

- Advertisement -