Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 16th, 2009, 09:28 AM
iand109 iand109 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 iand109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 45 sec
Reputation Power: 0
SQL Server 2005 - Using IF and ELSE in SQL statement

Hi,
I'm trying to use an IF condition within a SQL statement, but it's not working. It's trying to merge two columns ('KnownAsName' and 'surname') into one column called Name, but if there is no entry in the KnownAsName column, then it merges 'forename' and 'surname' as Name. I tried this:

Code:
IF [Staff.dbo.STAFF.KnownAsName] IS NOT NULL 
BEGIN
SELECT DISTINCT 
                      dbo.STAFF.FORENAME + ' ' + dbo.STAFF.SURNAME AS Name, Employment_1.EmpEnd, Employment_1.EmpStart, dbo.STAFF.Sex, dbo.STAFF.Manager, 
                      dbo.STAFF.EmployeeNo, dbo.STAFF.Profile, dbo.STAFF.Qual1, dbo.STAFF.Qual2, dbo.STAFF.Qual3, dbo.STAFF.Qual4, dbo.STAFF.Qual5, 
                      dbo.STAFF.Qual6, dbo.STAFF.Qual7, dbo.STAFF.Qual8, dbo.STAFF.Qual9, dbo.STAFF.Qual10, dbo.STAFF.SupressScores, 
                      dbo.STAFF.LastScoreSheetPrinted, dbo.STAFF.UID, Employment_1.ID, Employment_1.Centre, Employment_1.Position, dbo.STAFF.Photograph,
                          (SELECT     MIN(EmpStart) AS Expr1
                            FROM          dbo.Employment
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS EarliestStartDate
FROM         dbo.Employment AS Employment_1 LEFT OUTER JOIN
                      dbo.STAFF ON Employment_1.AppNo = dbo.STAFF.AppNo
WHERE     (Employment_1.EmpEnd IS NULL) AND (Employment_1.EmpStart < GETDATE()) OR
                      (Employment_1.EmpEnd > GETDATE())
END
ELSE
BEGIN
SELECT DISTINCT 
                      dbo.STAFF.FORENAME + ' ' + dbo.STAFF.SURNAME AS Name, Employment_1.EmpEnd, Employment_1.EmpStart, dbo.STAFF.Sex, dbo.STAFF.Manager, 
                      dbo.STAFF.EmployeeNo, dbo.STAFF.Profile, dbo.STAFF.Qual1, dbo.STAFF.Qual2, dbo.STAFF.Qual3, dbo.STAFF.Qual4, dbo.STAFF.Qual5, 
                      dbo.STAFF.Qual6, dbo.STAFF.Qual7, dbo.STAFF.Qual8, dbo.STAFF.Qual9, dbo.STAFF.Qual10, dbo.STAFF.SupressScores, 
                      dbo.STAFF.LastScoreSheetPrinted, dbo.STAFF.UID, Employment_1.ID, Employment_1.Centre, Employment_1.Position, dbo.STAFF.Photograph,
                          (SELECT     MIN(EmpStart) AS Expr1
                            FROM          dbo.Employment
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS EarliestStartDate
FROM         dbo.Employment AS Employment_1 LEFT OUTER JOIN
                      dbo.STAFF ON Employment_1.AppNo = dbo.STAFF.AppNo
WHERE     (Employment_1.EmpEnd IS NULL) AND (Employment_1.EmpStart < GETDATE()) OR
                      (Employment_1.EmpEnd > GETDATE())

END


but it came back saying multi-part identifier staff.dbo.staff.KnownAsName could not be bound (line 1)

Please can anyone help?

Reply With Quote
  #2  
Old October 16th, 2009, 02:13 PM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
ASP Free God 17th Plane (13000 - 13499 posts)
 
Join Date: Feb 2005
Location: Dreamland
Posts: 13,237 mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)mehere User rank is General 15th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 5 Months 1 Day 20 h
Reputation Power: 2012
you don't need the if/then statement, you could use a CASE statement in a single query. But I noticed that both your queries are exactly alike. Am I missing something?

Try something like this:
Code:
SELECT DISTINCT
	CASE 
		WHEN s.KnownAsName IS NOT NULL THEN s.KnownAsName + ' ' + s.SURNAME
		ELSE s.FORENAME + ' ' + s.SURNAME 
	END AS Name, 
	e.EmpEnd, e.EmpStart, 
	s.Sex, s.Manager, s.EmployeeNo, s.Profile, s.Qual1, s.Qual2, s.Qual3, s.Qual4, s.Qual5, 
	s.Qual6, s.Qual7, s.Qual8, s.Qual9, s.Qual10, s.SupressScores, s.LastScoreSheetPrinted, s.UID, 
	e.ID, e.Centre, e.Position, 
	s.Photograph,
		(SELECT MIN(EmpStart) AS Expr1
                FROM dbo.Employment e1
                WHERE s.AppNo = e1.AppNo)
                GROUP BY e1.AppNo) AS EarliestStartDate
FROM   dbo.Employment AS e
	LEFT OUTER JOIN dbo.STAFF s ON e.AppNo = s.AppNo
WHERE (e.EmpEnd IS NULL) AND (e.EmpStart < GETDATE()) 
	OR (e.EmpEnd > GETDATE())
__________________
Come JOIN the party!!!

Quote of the Month:
Pretension: The downside of being better than everyone else is that people tend to assume you're pretentious.

Questions to Ponder:
You can be overwhelmed and underwhelmed, but why can't you be simply whelmed?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright© 2008 sbenj69

Reply With Quote
  #3  
Old October 25th, 2009, 07:03 PM
gregory.owen@hp gregory.owen@hp is offline
Maniac
ASP Free Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Sweet Home, Oregon
Posts: 598 gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 14 h 46 m 46 sec
Reputation Power: 37
A simple solution is to use ISNULL:

SELECT ISNULL(KnownAsName,Forename) + ' ' + surname

If there is a possibility that forename can be null as well, you might want to use COELESCE instead:

SELECT COELESCE(KnownAsName, ForeName, 'default') + ' ' + surname.

That guarantees that something is printed for the first name.

Reply With Quote
  #4  
Old November 2nd, 2009, 04:46 AM
iand109 iand109 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 iand109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by gregory.owen@hp
A simple solution is to use ISNULL:

SELECT ISNULL(KnownAsName,Forename) + ' ' + surname

If there is a possibility that forename can be null as well, you might want to use COELESCE instead:

SELECT COELESCE(KnownAsName, ForeName, 'default') + ' ' + surname.

That guarantees that something is printed for the first name.


Thanks, coalesce worked a treat, nice one.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Server 2005 - Using IF and ELSE in SQL statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek