Solved

Trying to modify  this function in SQL that does a compare need help...

Posted on 2014-10-30
3
163 Views
Last Modified: 2014-10-30
I have this function below...
ALTER FUNCTION [dbo].[AddTwoValuesPhone](@LocId NVARCHAR(150),@DepId NVARCHAR(150))

RETURNS NVARCHAR(150)
AS
 BEGIN
        DECLARE @Result NVARCHAR(150)
        SET @Result = @LocId + ' - ' +  @DepId
        RETURN 
        (

            @Result
        )
    END

Open in new window


And what I'm trying to do it to
    What I would like to do
   
    if @Result is equal to 1 - 1 then the return result sould be this text string  = Utica/IT
   
   
    or if @Result is equal to 1 - 2 then the return result sould be this text string =  Utica/Accounting
0
Comment
Question by:yguyon28
[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
  • 2
3 Comments
 

Author Comment

by:yguyon28
ID: 40413541
Is it possible to write something like this?

@Result
                  CASE
                   WHEN @Result = '1 - 1' THEN "Hello"
                   ELSE @Result
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40413555
Give this a whirl..
ALTER FUNCTION [dbo].[AddTwoValuesPhone] (@LocId NVARCHAR(150), @DepId NVARCHAR(150))
RETURNS NVARCHAR(150)
AS
BEGIN
   DECLARE @Result NVARCHAR(150)
   SET @Result = CASE @LocId + ' - ' +  @DepId
      WHEN '1 - 1' THEN 'Utica/IT'
      WHEN '1 - 2' THEN 'Utica/Accounting' END
   RETURN @Result
END

Open in new window


Also, just to check the box, it's normally recommented to avoid using a udf for something this simple, as udf's cannot participate in a query's execution plan, which will affect performance.
0
 

Author Closing Comment

by:yguyon28
ID: 40414016
Perfect Thank you very much....
0

Featured Post

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.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

690 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