?
Solved

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

Posted on 2014-10-30
3
Medium Priority
?
169 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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