I am using SQL Server 2014 and I have the following CASE statement in a T-SQL query.
(CASE
WHEN c.[Market FINAL] = 'Overbooking' AND c.[Booking type] = 'GRP' THEN 'Overbooking'
WHEN c.[TaProfileID] = 853 THEN 'Poland (TUI only)'
WHEN c.[Source of Business] = 'TO' AND c.[CountryName] = 'Netherlands' THEN 'Netherlands'
WHEN c.[Source of Business] = 'DMC' AND c.[Booking Origin (1)] = 'Netherlands' THEN 'Netherlands'
WHEN c.[Booking type] = 'GRP' THEN 'G&I'
ELSE c.[Market FINAL]
END) AS 'Market',
I would like to remove it from this query and run it as a user-defined function but I am having a hard time creating the function. Also, how would I use it in my query once it is running as a function?
CREATE FUNCTION [dbo].[_fnMyCase] ( @Filter1 AS VARCHAR(20) , @Filter2 AS VARCHAR(20)) RETURNS VARCHAR(20) AS BEGIN DECLARE @MyReturn AS VARCHAR(20) = '' SELECT @MyReturn = (CASE WHEN c.[Market FINAL] = 'Overbooking' AND c.[Booking type] = 'GRP' THEN 'Overbooking' WHEN c.[TaProfileID] = 853 THEN 'Poland (TUI only)' WHEN c.[Source of Business] = 'TO' AND c.[CountryName] = 'Netherlands' THEN 'Netherlands' WHEN c.[Source of Business] = 'DMC' AND c.[Booking Origin (1)] = 'Netherlands' THEN 'Netherlands' WHEN c.[Booking type] = 'GRP' THEN 'G&I' ELSE c.[Market FINAL] END) FROM c WHERE Filter1 = @Filter1 AND Filter2 = @Filter2 RETURN @MyReturn END
This logic looks like design flaw. However, since all columns are in the same table, I would suggest computed column:
CREATE TABLE SomeTable
(
[Market Final] varchar(20),
[Booking type] varchar(10),
TaProfileId int,
[Source of Business] varchar(10),
[CountryName] varchar(20),
[Booking Origin (1)] varchar(10),
--Other fields
Market AS
CASE
WHEN [Market FINAL] = 'Overbooking' AND [Booking type] = 'GRP' THEN 'Overbooking'
WHEN [TaProfileID] = 853 THEN 'Poland (TUI only)'
WHEN [Source of Business] = 'TO' AND [CountryName] = 'Netherlands' THEN 'Netherlands'
WHEN [Source of Business] = 'DMC' AND [Booking Origin (1)] = 'Netherlands' THEN 'Netherlands'
WHEN [Booking type] = 'GRP' THEN 'G&I'
ELSE [Market FINAL]
END
)
Then, instead of your complex expression, you can use:
SELECT c.Market, ...
FROM SomeTable c
You should create a function like the below.
CREATE FUNCTION [dbo].[fnMyCase] (
@MarketFINAL VARCHAR(50)
,@Bookingtype VARCHAR(50)
,@TaProfileID INT
,@SourceofBusiness VARCHAR(50)
,@CountryName VARCHAR(50)
,@BookingOrigin VARCHAR(50)
)
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @MyReturn AS VARCHAR(50) = ''
SELECT @MyReturn = (
CASE
WHEN @MarketFINAL = 'Overbooking'
AND @Bookingtype = 'GRP'
THEN 'Overbooking'
WHEN @TaProfileID = 853
THEN 'Poland (TUI only)'
WHEN @SourceofBusiness = 'TO'
AND @CountryName = 'Netherlands'
THEN 'Netherlands'
WHEN @SourceofBusiness = 'DMC'
AND @BookingOrigin = 'Netherlands'
THEN 'Netherlands'
WHEN @Bookingtype = 'GRP'
THEN 'G&I'
ELSE @MarketFINAL
END
)
RETURN @MyReturn
END
Call the function like this,
SELECT *
,[dbo].[fnMyCase](c.[Market FINAL], c.[Booking type], c.[TaProfileID], c.[Source of Business], c.[CountryName], c.[Booking Origin (1)]) AS Market
FROM c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.