?
Solved

In SQL query how to use If statement that if column value blank then assign value Empty else Not Empty

Posted on 2010-01-12
12
Medium Priority
?
241 Views
Last Modified: 2013-11-30
Hi All,
I have a select query
Select FirstName, LastName, Address, Type
from Employee

In Type colum some of the row are blanks, I need to set those row as Contract
I.e If Type is Null then Contract else Permanent.
0
Comment
Question by:mas1963
[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
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26294319
Select FirstName, LastName, Address, case when Type is null then 'Contract' else 'Permanent' end as type
from Employee
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 26294326
Select FirstName, LastName, Address, CASE WHEN [Type] IS NULL THEN 'Contract' ELSE 'Permanent' END AS [Type]
from Employee
0
 

Author Comment

by:mas1963
ID: 26294524
I have getting error Invalid column name PD.POPDEP

CASE WHEN [PD.POPDJB] IS NULL THEN 'OpEX' ELSE 'CapEX' END AS [PD.POPDJB]
0
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.

 
LVL 56

Expert Comment

by:Bill Prew
ID: 26294533
Just thought I'd mention, you also have the ISNULL() function.  So if all you wanted to do was replace the empty values with something, and leave the non empty ones alone you can do:

Select FirstName, LastName, Address, ISNULL(Type, 'Contract') from Employee

http://msdn.microsoft.com/en-us/library/aa933210%28SQL.80%29.aspx

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 26294557
Try this:

CASE WHEN [PD.POPDJB] IS NULL THEN 'OpEX' ELSE 'CapEX' END AS 'POPDJB'

~bp
0
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 80 total points
ID: 26294580
The other way to write it is:

SELECT
POPDJB = CASE WHEN [PD.POPDJB] IS NULL THEN 'OpEX' ELSE 'CapEX' END
FROM Table1

~bp
0
 

Author Comment

by:mas1963
ID: 26294726
Following is full query and in POPDJB column it just displaying CapEX

SELECT     G.GLDGL AS Account, P.POPOID AS PONumber, G.GLDEDT AS EntryDate, A.APTRN1 AS InvoiceTrans, A.APTEDT AS EntryDate,
                      A.APTEDS AS Description, A.APTSU1 AS InvoiceStatus, A.APTBA6 AS APAmount, A.APTCKN AS Cheque#, A.APTDT4 AS ChequeDate,
                      A.APTDT5 AS ClearDate, G.GLDBA AS Amount, A.APTCKN AS CheckNum, A.APTDT4 AS CheckDate, A.APTDT5 AS ClearDate, G.GLDJB AS Job,
                      G.GLDGL AS Costcode, G.GLDBT AS JVN, PD.POPDEP AS Amount, PD.POPDJB as Type,
                              POPDJB = CASE WHEN PD.POPDJB IS NULL THEN 'OpEX' ELSE 'CapEX' END
FROM         dbo.bpl_GLPD AS G LEFT OUTER JOIN
                      dbo.bpl_POPPD AS PD ON G.GLDPJ = PD.POPDPJ LEFT OUTER JOIN
                      dbo.bpl_APPT AS A ON G.GLDEN1 = A.APTEC1 AND G.GLDRN1 = A.APTRN1 AND G.GLDBT = A.APTBT AND G.GLDBE = A.APTBE LEFT OUTER JOIN
                      dbo.bpl_POPPO AS P ON G.GLDTM = P.POPOTM
WHERE     (G.GLDBK = 'ACT') AND (G.GLDJB = 'BT09ADMIG') AND (G.GLDEDT >= '1080101') AND (G.GLDEDT <= '1091231')
ORDER BY G.GLDCC, G.GLDJB, G.GLDEDT, G.GLDJC, G.GLDBT, G.GLDBE, G.GLDBL
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 26294755
Perhaps there are no NULLs in that field?  Maybe they are blanks, and not nulls?  Try a query just looking for nulls in that field.

~bp
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 120 total points
ID: 26294772
mas1963 said:
>>Following is full query and in POPDJB column it just displaying CapEX

That tells me that POPDJB is not truly NULL, and instead it has either a zero-length string or all spaces.

Try:

SELECT     G.GLDGL AS Account, P.POPOID AS PONumber, G.GLDEDT AS EntryDate, A.APTRN1 AS InvoiceTrans, A.APTEDT AS EntryDate,
                      A.APTEDS AS Description, A.APTSU1 AS InvoiceStatus, A.APTBA6 AS APAmount, A.APTCKN AS Cheque#, A.APTDT4 AS ChequeDate,
                      A.APTDT5 AS ClearDate, G.GLDBA AS Amount, A.APTCKN AS CheckNum, A.APTDT4 AS CheckDate, A.APTDT5 AS ClearDate, G.GLDJB AS Job,
                      G.GLDGL AS Costcode, G.GLDBT AS JVN, PD.POPDEP AS Amount, PD.POPDJB as Type,
                              POPDJB = (CASE WHEN RTRIM(COALESCE(PD.POPDJB, '')) = '' THEN 'OpEX' ELSE 'CapEX' END)
FROM         dbo.bpl_GLPD AS G LEFT OUTER JOIN
                      dbo.bpl_POPPD AS PD ON G.GLDPJ = PD.POPDPJ LEFT OUTER JOIN
                      dbo.bpl_APPT AS A ON G.GLDEN1 = A.APTEC1 AND G.GLDRN1 = A.APTRN1 AND G.GLDBT = A.APTBT AND G.GLDBE = A.APTBE LEFT OUTER JOIN
                      dbo.bpl_POPPO AS P ON G.GLDTM = P.POPOTM
WHERE     (G.GLDBK = 'ACT') AND (G.GLDJB = 'BT09ADMIG') AND (G.GLDEDT >= '1080101') AND (G.GLDEDT <= '1091231')
ORDER BY G.GLDCC, G.GLDJB, G.GLDEDT, G.GLDJC, G.GLDBT, G.GLDBE, G.GLDBL


That will check for NULL, all spaces, or zero length string.
0
 

Author Comment

by:mas1963
ID: 26294848
If field is blank can I use same stement
POPDJB = CASE WHEN [PD.POPDJB] IS BLANK THEN 'OpEX' ELSE 'CapEX' END
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 26294869
mas1963 said:
>>If field is blank can I use same stement
>>POPDJB = CASE WHEN [PD.POPDJB] IS BLANK THEN 'OpEX' ELSE 'CapEX' END

No
0
 

Author Comment

by:mas1963
ID: 26294941
Thanks everyone for help
matthewspatrick your check for NULL, all spaces, or zero length string work fine.

Thanks again
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.
Suggested Courses

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