Find result using two values in function

Posted on 2009-04-24
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
    LVL 60

    Expert Comment

    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

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

    Accepted Solution

    like this:

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now