Solved

if ... field is null then field = 'Some Text'

Posted on 2000-05-09
10
356 Views
Last Modified: 2012-05-04
part of my sql statement is

select jobs.sqlkey, jobs.Job__, jobs.Name, jobfilelocations.person,  
jobfilelocations.datetaken, FileBIN = JobFileLocations.sqlKey from jobs
left outer join Jobfilelocations on jobs.sqlkey = jobfilelocations.jobIDx and
(returned = 0 or returned is null)

I want to add something that would make the jobfilelocations.person = 'Filed'  if the (retruned is null)

the rest of the sql statement works perfectly, I just need to add something that I am not sure how to do
0
Comment
Question by:RoLaAus
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:bbychkov
Comment Utility
You can use IsNull in the select list:
select IsNull(jobfilelocations.person, 'Filed') As Person
or you can use CASE:
select CASE WHEN returned IS NULL THEN 'Filed' ELSE jobfilelocations.person END from ...


   
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
Comment Utility
Your syntax would be like this:

select jobs.sqlkey, jobs.Job__, jobs.Name,
PersonValue = CASE
  WHEN returned Is Null Then 'Filed'
  ELSE jobfilelocations.person
END,  
jobfilelocations.datetaken, FileBIN = JobFileLocations.sqlKey from jobs
left outer join Jobfilelocations on jobs.sqlkey = jobfilelocations.jobIDx and
(returned = 0 or returned is null)


The IsNull would not work for this, since it would return the Returned value if returned was not null, and you need the value in the field person.
0
 
LVL 4

Expert Comment

by:Gustavo Perez Buenrostro
Comment Utility
RoLaAus,

If I understood your request you should do something like this:

select jobs.sqlkey
      ,jobs.Job
      ,jobs.Name
      ,jobfilelocations.person
      ,jobfilelocations.datetaken
      ,FileBIN=JobFileLocations.sqlKey
  from jobs
       join Jobfilelocations
         on jobs.sqlkey=jobfilelocations.jobIDx
 and (returned=0 or returned is null)
 and (jobfilelocations.person
     =case when returned is null
        then 'Filed'
        else jobfilelocations.person
      end)
0
 
LVL 4

Expert Comment

by:Gustavo Perez Buenrostro
Comment Utility
bhess1,
I didn’t reload the question before my comment was posted.
0
 
LVL 1

Expert Comment

by:bbychkov
Comment Utility
bhess1,
If "returned is null" states for records from jobs that don't have matching records in jobfilelocations (remember, it's left outer join), then jobfilelocations.person is null for these records as well. Then "IsNull(jobfilelocations.person, 'Filed') As Person" will work.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:RoLaAus
Comment Utility
bbychkov,

is the 'left outer join' appropriate for this?

I am only vaguely familiar with join statements, and usually build the sql using the query building in Access then convert any syntax over to be compatible in SQL

so I am not sure which JOIN type is appropriate, all I know right now is that it work <G>  but it might work better, and I am not aware of it.

I will grade your comment as soon as I verify that it works (I switched projects temporarily), but it looks fine - logic wise
0
 

Author Comment

by:RoLaAus
Comment Utility
thanks, it worked like a charm

I looked for the IF statement in the SQL help in Enterprise Manager, but it didn't give a sample statement

Of course, I should have looked for CASE (had I known about it) but figured i would find the answer quicker here
0
 
LVL 1

Expert Comment

by:bbychkov
Comment Utility
RoLaAus,
left outer join will give you all records from jobs table and only those records from Jobfilelocations table that have jobfilelocations.jobIDx = jobs.sqlkey. If record from jobs doesn't have matching record in Jobfilelocations then all fileds from Jobfilelocations will be returned as NULL (and IsNull function will replace them with value that you specify). If you need only records from jobs table that have matches in Jobfilelocations then you have to use inner join.
0
 

Author Comment

by:RoLaAus
Comment Utility
I guess I should have clearified, I do need all job records

we need to know if certain files are on peoples desk (returned would be 0 and their name in the Person field)

or if no one has the job, it would be in the Files - that is why I wanted to add the 'Filed'  into the person field, ppl were getting confused by a blank field wondering where the file is located.

thanks again
0
 
LVL 1

Expert Comment

by:bbychkov
Comment Utility
Than left outer join is fine. And both IsNull and CASE will do it for you.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

744 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

12 Experts available now in Live!

Get 1:1 Help Now