Solved

sql else statement

Posted on 2007-04-11
5
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 33
get count of orders by customer Sql Server table. 3 48
VM SQL server license. 1 64
relocating SQL 2000 18 30
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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