Solved

sql else statement

Posted on 2007-04-11
5
172 Views
Last Modified: 2010-03-20
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.
0
Comment
Question by:Soluga
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18888541
1) Please describe what you are trying to do

2) Please post the whole SQL statement so we can get the context
0
 
LVL 1

Author Comment

by:Soluga
ID: 18888571
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
0
 
LVL 11

Expert Comment

by:Sven
ID: 18888709
(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...
0
 
LVL 11

Accepted Solution

by:
Sven earned 500 total points
ID: 18888717
I got some error in my syntax or there is some more in yours ;) Try this one:

(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),
 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)
 else 'N/A'
 END AS [Date Trained]
 FROM dbo.candidates

I think the "AS InviteDate" is not needed and you want to get only a field named "Date Trained".
0
 
LVL 1

Author Comment

by:Soluga
ID: 18888830
Cheers
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now