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
 Please help me Debug

Author  Topic 

Sarathchandra
Starting Member

2 Posts

Posted - 2015-03-28 : 08:01:02
Hi, I am new to SQl I have written a code which is as follows but gives an error
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'SALARY'.


Please find the code below

Create Table Person(ID INT, NAME NVARCHAR(255),SALARY INT, JDATE DATETIME,CITY NVARCHAR(255),REGION CHAR(255))
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(1,'MEGAN',25000, '02/01/96','UTAH','W')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(2,'BRUCE',35000, '02/05/96','NEWYORK','E')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(3,'KAI',27000, '02/06/97','CHICAGO','S')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(4,'TYSON',28000, '02/01/96','TEXAS','W')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(5,'BROOKLYN',29000, '02/07/94','BROOKLYN','W')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(6,'RAY',30000, '02/01/98','PARIS','W')
insert into Person(ID,NAME,SALARY,JDATE,CITY,REGION)VALUES(7,'MAX',31000, '22/1/96','LONDON','W')
SELECT * FROM Person
SELECT ID
CASE
WHEN SALARY=30000 THEN 6
WHEN SALARY=35000 THEN 2
ELSE
NO SALARY PRESENT
END
go


Please help me with the correct syntax


Sarath chandra

Sarath chandra

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-03-28 : 08:20:09
SELECT ID
CASE
WHEN SALARY=30000 THEN '6'
WHEN SALARY=35000 THEN '2'
ELSE
'NO SALARY PRESENT'
END
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-03-28 : 11:17:34
Further refinement:

SELECT ID,
CASE
WHEN SALARY=30000 THEN '6'
WHEN SALARY=35000 THEN '2'
ELSE
'NO SALARY PRESENT'
END AS MyColumnName
Go to Top of Page
   

- Advertisement -