Link to home
Start Free TrialLog in
Avatar of Soluga
Soluga

asked on

sql else statement

Hi
I am trying to resolve an else statement but I keep getting syntax errors....
                        (SELECT TOP 1 lpSignDateTime FROM dbo.lpSign
                              WHERE planVersion = @intPlanversion AND planID = @intplanID AND userID = dbo.candidates.candidateID isNull Then 'NA') END 'Date Trained'
                        else
                        (SELECT TOP 1 lpSignDateTime FROM dbo.lpSign
                              WHERE planVersion = @intPlanversion AND planID = @intplanID AND userID = dbo.candidates.candidateID ORDER BY lpsigndatetime) END 'Date Trained'

Grateful for any help thanks.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

1) Please describe what you are trying to do

2) Please post the whole SQL statement so we can get the context
Avatar of Soluga
Soluga

ASKER

I am trying to check for a null value in a date field, so if there is a null I can send back an 'N/A'. The code below now runs, but I get
Server: Msg 241, Level 16, State 1, Line 27
Syntax error converting datetime from character string.

                  (SELECT TOP 1 esDate FROM dbo.lpAssignDetails
                        WHERE planVersion = @intPlanversion AND planID = @intplanID and planFinished = 0 or planFinished = 1 AND userID = dbo.candidates.candidateID ORDER BY esDate) AS 'Invite Date',
                  CASE WHEN lps.lpSignDateTime IS NOT NULL THEN
                        (SELECT TOP 1 lpSignDateTime FROM dbo.lpSign
                              WHERE planVersion = @intPlanversion AND planID = @intplanID AND userID = dbo.candidates.candidateID)
                        else 'N/A'
                         END 'Date Trained'
            FROM         dbo.candidates
(SELECT TOP 1 esDate FROM dbo.lpAssignDetails
 WHERE planVersion = @intPlanversion AND planID = @intplanID and planFinished = 0 or planFinished = 1 AND userID = dbo.candidates.candidateID ORDER BY esDate) AS InviteDate,
 CASE WHEN NOT lps.lpSignDateTime IS NULL THEN
 (SELECT TOP 1 lpSignDateTime FROM dbo.lpSign
 WHERE planVersion = @intPlanversion AND planID = @intplanID AND userID = dbo.candidates.candidateID) AS InviteDate
 else 'N/A' AS InviteDate
 END, [Date Trained]
 FROM dbo.candidates

Table or Field Alias must not be in single quotation marks and must not contain spaces (AS InviteDate instead of AS 'Invite Date'). You forgot the "AS InviteDate" on the second SELECT and 'N/A'.

You can also try "AS [Invite Date]" if you want to keep the space in field name. Use the brackets [] instead of single quotation marks.

Also I think you have to use "NOT lps.lpSignDateTime IS NULL" instead of "lps.lpSignDateTime IS NOT NULL".

What is 'Date Trained'? Is this another field of the table? Then you have to use this:  ...END, [Date Trained] FROM...
ASKER CERTIFIED SOLUTION
Avatar of Sven
Sven
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Soluga

ASKER

Cheers