Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of IF and Case

Status
Not open for further replies.

rlatham

Programmer
Aug 23, 2000
51
US
Please, can someone tell me what is wrong in the code below?
I am just starting to learn SQL, (by myself) and need some guidance... Thank you.

I get this error messages:
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'IF'.
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'CASE'.

Code:
SELECT     dbo.MAP_IDerrors_DemogMatch.*, dbo.StudentsID_Src.ID_Number AS StID
FROM         dbo.StudentsID_Src INNER JOIN
                      dbo.MAP_IDerrors_DemogMatch ON dbo.StudentsID_Src.SetID = dbo.MAP_IDerrors_DemogMatch.SetID
WHERE     dbo.StudentsID_Src.ID_Number = IF (len(idno) = 9) CASE len(old_id_number) WHEN 5 THEN concat('00', old_id_number) WHEN 6 THEN concat('0', 
                      old_id_number) ELSE CASE len(idno) WHEN 5 THEN concat('00', idno) WHEN 6 THEN concat('0', idno)

 
hi,

Does this query return wht u r lokigng ofr...

SELECT dbo.MAP_IDerrors_DemogMatch.*, dbo.StudentsID_Src.ID_Number AS StID
FROM
dbo.StudentsID_Src INNER JOIN
dbo.MAP_IDerrors_DemogMatch ON dbo.StudentsID_Src.SetID = dbo.MAP_IDerrors_DemogMatch.SetID
WHERE dbo.StudentsID_Src.ID_Number =
CASE len(idno) WHEN 9 THEN
CASE len(old_id_number)
WHEN 5 THEN '00'+ old_id_number
WHEN 6 THEN '0'+ old_id_number
END
ELSE
CASE len(idno) WHEN 5 THEN '00'+ idno
WHEN 6 THEN '0' + idno
END
END

Sunil
 
Thank you very much Sunil.
I does exactly what I needed and I learned a little more also.

[bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top