SQL Server - Can't figure out the syntax for this IF statement

Asked By Zack G
11-Apr-08 02:50 AM
Hi,

I have a CASE statement that is something like:

If EMPLOYEEIDB is not null, then EMPLOYEEIDA/EMPLOYEEIDB, else
EMPLOYEEIDA.

My problem is that I do not actually want to print the employee IDs. I
want to join the table them to the table contaning the full employee
names and print those instead. How can I do this?

Thanks.
SQL Server 2000
(1)
SQL Server 2005
(1)
SQL Server
(1)
ACaseManagerID
(1)
CREATE TABLE
(1)
PRIMARY KEY
(1)
DROP TABLE
(1)
NOT NULL
(1)
  Erland Sommarskog replied...
10-Apr-08 05:47 PM
Zack G (z.gallinger@gmail.com) writes:

SELECT e.FirstName, e.LastName
FROM   Employee e
JOIN   Zackstable z ON coalesce(z.employeeidb, z.employeeida) = e.employeeid

coalesce(expr1, expr2, ... exprn) is short for

CASE WHEN expr1 IS NOT NULL THEN expr1
WHEN expr2 IS NOT NULL THEN expr2
...
WHEN exprn IS NOT NULL THEN exprn
ELSE NULL
END

By the way, there is no CASE statement in SQL. Only a CASE expression.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
  Zack G replied...
12-Apr-08 08:39 PM
I don't think that this is the answer. I probably did not make myself
clear enough in my first post.

I need a solution such as this

CASE WHEN ProjectManagerID IS NOT NULL THEN CaseManagerID + '/' +
ProjectManagerID ELSE CaseManagerID END

but instead of printing the IDs, I need to print the first name and
last names of the employees.

Can anyone help me out?



oyeeid
chnol/sql/2005/downloads/books...
reviousversions/books.mspx
  Jason Lepack replied...
12-Apr-08 08:39 PM
Hello Zack G,

Please post DDL, sample data, and expected results to receive an
accurate solution.

Cheers,
Jason Lepack
  Zack G replied...
12-Apr-08 08:39 PM
Sample data

Table A

CaseManagerID ProjectManagerID

AAAA11          NULL
BBBB11          NULL
CCCC11          DDDD11

Table B

EmployeeID   FirstName   LastName
AAAA11        Joe             Smith
BBBB11        Mary           Best
CCCC11        Jim             Todd
DDDD11        Sam           Jones


Expected Results

Joe Smith
Mary Best
Jim Todd/Sam Jones


I hope this helps.

I

employeeid

odtechnol/sql/2005/downloads/books...
fo/previousversions/books.mspx- Hide quoted text -
  --CELKO-- replied...
12-Apr-08 08:39 PM
This is not DDL; it is a vague narrative in which data elements change
names from table to table.  When a data element changes roles, you
prefix it with a role name.  Tables always have a PRIMARY KEY and
almost alway needs more constraints.  Your expected results have no
name and no column name, or any specs to tell us what you want.  Even
worse, you show two concatenated names in the results in violation of
1NF!  You made the project manager NULL-able; a better approach would
be a TBD ('to be determined') token so you can get a good PK

CREATE TABLE ProjectAssignments
(case_mgr_emp_id CHAR(6) NOT NULL
REFERENCES Personnel(emp_id),
proj_mgr_emp_id CHAR(6) DEFAULT 'XXXX11' NOT NULL
REFERENCES Personnel(emp_id),
PRIMARY KEY (case_mgr_emp_id, proj_mgr_emp_id)
etc.);

CREATE TABLE Personnel
(emp_id CHAR(6) NOT NULL PRIMARY KEY
CHECK (emp_id LIKE '[A-Z][A-Z][A-Z][A-Z][1-9][1-9]'),
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15) NOT NULL,
etc.);

-- a dummy value
INSERT INTO Personnel VALUES (''XXXX11', '{{TBD}}', '{{TBD}}');

Let's give the user normalized results with column names:

SELECT (P1.last_name +' '+ P1.first_name) AS case_mgr_name,
(P2.last_name +' '+ P2.first_name) AS proj_mgr_name
FROM Personnel AS P1, Personnel AS P2, ProjectAssignments AS A
WHERE P1.emp_id = A.case_mgr_emp_id
AND P2.emp_id = A.proj_mgr_emp_id;
  Zack G replied...
12-Apr-08 08:39 PM
I didn't make these tables. They are a part of an accounting system. I
am just trying to create a report based on them. I'll try explaining
myself again.

Here are my tables

Table A

Project # CaseManagerID ProjectManagerID

1             AAAA11          NULL
2             BBBB11          NULL
3             CCCC11          DDDD11

Table B

EmployeeID   FirstName   LastName
AAAA11        Joe             Smith
BBBB11        Mary           Best
CCCC11        Jim             Todd
DDDD11        Sam           Jones


I simply need a statement such as this one
Select Project#, CASE WHEN ProjectManagerID IS NOT NULL THEN
CaseManagerID + '/' +
ProjectManagerID ELSE CaseManagerID END AS Managers From Table A LEFT
OUTER JOIN Table B ON A.(Not sure what to put here) =3D B.EmployeeID".


However, instead of printing the IDs, I would like to print a first
and last name as can be seen below

Project #    Managers
1               Joe Smith
2               Mary Best
3               Jim Todd/Sam Jones

Please let me know if any further clarification is required.
  Aaron Bertrand [SQL Server MVP] replied...
11-Apr-08 02:16 PM
CREATE TABLE #TableA
(
Project# INT PRIMARY KEY,
CaseManagerID CHAR(6),
ProjectManagerID CHAR(6)
);

CREATE TABLE #TableB
(
EmployeeID CHAR(6),
FirstName VARCHAR(20),
LastName VARCHAR(20)
);

SET NOCOUNT ON;

INSERT #TableA(Project#,FirstName,LastName)
SELECT 1,'AAAA11',NULL
UNION SELECT 2,'BBBB11',NULL
UNION SELECT 3,'CCCC11','DDDD11';

INSERT #TableB(EmployeeID,FirstName,LastName)
SELECT 'AAAA11','Joe','Smith'
UNION SELECT 'BBBB11','Mary','Best'
UNION SELECT 'CCCC11','Jim','Todd'
UNION SELECT 'DDDD11','Sam','Jones';

-- assuming FirstName and LastName can not be NULL
SELECT
Project#,
Managers = B.FirstName + ' ' + B.LastName +
COALESCE('/' + C.FirstName + ' ' + C.LastName, '')
FROM #TableA A
LEFT OUTER JOIN #TableB B
ON B.EmployeeID = A.CaseManagerID
LEFT OUTER JOIN #TableB C
ON C.EmployeeID = A.ProjectManagerID
ORDER BY 1;

DROP TABLE #TableA, #TableB;




I didn't make these tables. They are a part of an accounting system. I
am just trying to create a report based on them. I'll try explaining
myself again.

Here are my tables

Table A

Project # CaseManagerID ProjectManagerID

1             AAAA11          NULL
2             BBBB11          NULL
3             CCCC11          DDDD11

Table B

EmployeeID   FirstName   LastName
AAAA11        Joe             Smith
BBBB11        Mary           Best
CCCC11        Jim             Todd
DDDD11        Sam           Jones


I simply need a statement such as this one
Select Project#, CASE WHEN ProjectManagerID IS NOT NULL THEN
CaseManagerID + '/' +
ProjectManagerID ELSE CaseManagerID END AS Managers From Table A LEFT
OUTER JOIN Table B ON A.(Not sure what to put here) = B.EmployeeID".


However, instead of printing the IDs, I would like to print a first
and last name as can be seen below

Project #    Managers
1               Joe Smith
2               Mary Best
3               Jim Todd/Sam Jones

Please let me know if any further clarification is required.
  Zack G replied...
12-Apr-08 08:39 PM
Thanks!
Create New Account
help
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit when
Why_can’t_recursive_queries_contain. . .? SQL Server hi I hope I didn = 92t put too many questions 1) Why can = 92t recursive queries also be unioned together with UNION operator ( instead they must use UNION ALL )? thanx SQL Server Programming Discussions SQL Server 2008 (1) SQL Server 2005 (1) SQL Server 2000 (1) SQL Server (1) Oracle (1) Ruby (1) MichaelcoAToptonlineDOTnet (1
Access 2007-> SQL Server2005 "connection was forcibly closed", GNE 1 SQL Server SQL, Server2005, "connection, was, forcibly, closed", GNE, 1" / > Hi, with an Access 2007 application, I have a very big problem connecting an SQL Server 2005. The scenario: - nearly 200 clients with Windows XP Professional (in an Active Directory Domain) - clients uses WAN, LAN and WLAN, different locations - one SQL Server 2005 EE, uses actually 36 databases, the Access 2007 application uses 2 of them
SQL Express: Failed Reinstall SQL Server Dear Setup Experts: I am trying to reinstall SQL Express after playing with it some. I uninstalled it, and now, it refuses to reinstall in the face. Any ideas what this log file means and how I can get SQL Express installed? I really do not want to have to reinstall my whole system. * ** ** Start of Log File Overall summary: Final result: SQL Server installation failed. To continue, investigate the reason for the failure, correct the problem, uninstall SQL Server, and then rerun SQL Server Setup. Exit code (Decimal): -2068643839 Exit facility code: 1203
Cannot restart SQL Server service SQL Server I recently installed SQL Server 2008 Express Edition with Advanced Tools. This is the version information from SQL Server Management Studio Microsoft SQL Server Management Studio 10.0.1600.22 ((SQL_PreRelease).080709- 1414 ) Microsoft