Find result using two values in function

Posted on 2009-04-24
Medium Priority
Last Modified: 2012-05-06
I have two fields in a table status1 and status2.  I would like to create a function where if status1 was = A and status2 was = B the result would give me value A1 or if status1 = B and status2 = A the result would be value A2.  How do I do this with a function?
Question by:Fairfield
  • 2
LVL 60

Expert Comment

ID: 24227392
create function dbo.myfunction
@status1 char(1),
@status2 char(1)
returns char(2)
declare @ret char(2)
set @ret = case when @status1 = 'A' and @status2 = 'B' then 'A1' when @status1 = 'B' and @status2 = 'A' then 'A2' end


Author Comment

ID: 24227759
How would I use this function in a select statement?
LVL 60

Accepted Solution

chapmandew earned 2000 total points
ID: 24227770
like this:

select dbo.myfunction(statusfield1fromtable, statusfield2fromtable), *
from tablename


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

850 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