Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If there has ever been a justification needed for access to the net during working hours, just referring to this site should suffice. Fantastic!..."

Geography

Where in the world do Tek-Tips members come from?
wbodger (Programmer)
3 Jul 12 10:45
I have a function with the following query that may or may not have any records, but I need to return a value regardless

CODE

with optoutcte AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY OptOutDate DESC) AS Ordered
FROM tblSiteTaskOptOuts
WHERE SiteID=siteid
and TypeID=1
)
select ISNULL(OptOut,0) AS Status 
from optoutcte where Ordered=1 

I was hoping that the ISNULL would work when there were no records in the CTE, but no luck. Any thoughts? I will be passing in a SiteID and there may not be any records. OptOut is a bit type field, so it can be 0 or 1. If there is no record, I want it to return a 0. Help?

wb
Helpful Member!  bborissov (Programmer)
3 Jul 12 11:05

CODE

IF NOT EXISTS(SELECT * FROM tblSiteTaskOptOuts WHERE SiteID=siteid and TypeID=1)
   SELECT CAST(0 AS bit) AS Status
ELSE
   SELECT OptOut
          FROM (SELECT OptOut, ROW_NUMBER() OVER (ORDER BY OptOutDate DESC) AS Ordered
                      FROM tblSiteTaskOptOuts
                WHERE SiteID=siteid
                  and TypeID=1) Tbl1
   WHERE Ordered = 1 

Borislav Borissov
VFP9 SP2, SQL Server

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close