Solved

SQL 2008 How to specify column name when using case statement

Posted on 2014-01-15
5
2,810 Views
Last Modified: 2014-01-15
This should be simple... How to specify a column name "as ....." when using a case statement (in bold below).

COALESCE(T1.POSTCODE_FW, '') as 'Zip Code',
COALESCE(T1.VENDOR_SPECIALTY_FW, '') as 'Vendor Specialty',
COALESCE(T1.VENDOR_TYPE_FW, '') as 'Vendor Type',

CASE
      WHEN T1.SUPSTATUS_FW = '0' THEN ''
      WHEN T1.SUPSTATUS_FW = '1' THEN 'YES'
      ELSE ''
END,


COALESCE(CONVERT(varchar(10), T2.START_DATE_FW, 101), '') as 'Start Date',
COALESCE(CONVERT(varchar(10), T2.END_DATE_FW, 101), '') as 'End Date',
0
Comment
Question by:66chawger
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
Comment Utility
CASE
      WHEN T1.SUPSTATUS_FW = '0' THEN ''
      WHEN T1.SUPSTATUS_FW = '1' THEN 'YES'
      ELSE ''
END   As NewColumnName ,
0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 125 total points
Comment Utility
CASE T1.SUPSTATUS
   WHEN '1' THEN 'YES'
   ELSE ''
END AS SomeColumnName,
0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 125 total points
Comment Utility
You can assign a fieldname two ways
1. fieldname = calculation, or
2. calculation as fieldname

My preference is the first option, so you don't have to look at the end of a really long line:
--example 1:
SELECT	SomeColumnName =	CASE  T1.SUPSTATUS_FW 
					WHEN '1' THEN 'YES'
					ELSE ''
				END
FROM	SomeTable

--example 2:
SELECT	CASE  T1.SUPSTATUS_FW 
		WHEN '1' THEN 'YES'
		ELSE ''
	END AS SomeColumnName
FROM	SomeTable

Open in new window

0
 

Author Comment

by:66chawger
Comment Utility
Everyone, all of these were right on... I had the syntax incorrect.  I going to split up the points as everyone provided great info.
0
 

Author Closing Comment

by:66chawger
Comment Utility
Awarded Aneesh half of the 500 as they were the first one to respond.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

8 Experts available now in Live!

Get 1:1 Help Now