October 13, 2014 at 3:25 am
Hi
Please assist with a SQL Function.
I have the following data table:
DataTable ([Lookup]) as
(
select '1 2' [Lookup] union all
select '2 3' [Lookup] union all
select '3 1' [Lookup]
)
Then I have the following Lookup Table:
LookupTable ([ID], [Descr]) as
(
select 1 [ID], 'Red' [Descr] union all
select 2 [ID], 'Green' [Descr] union all
select 3 [ID], 'Blue' [Descr]
)
I want to create a SQL Function that will give me the following results as I currently have in the Function column. Please run my query below to see results. I do not want to use the Case Statement anymore. I want to replace this with a SQL Function.
with LookupTable ([ID], [Descr]) as
(
select 1 [ID], 'Red' [Descr] union all
select 2 [ID], 'Green' [Descr] union all
select 3 [ID], 'Blue' [Descr]
),
DataTable ([Lookup]) as
(
select '1 2' [Lookup] union all
select '2 3' [Lookup] union all
select '3 1' [Lookup]
)
select
d.[Lookup] [Lookup],
case when d.[Lookup] = '1 2'
then 'Red, Green'
when d.[Lookup] = '2 3'
then 'Green Blue'
when d.[Lookup] = '3 1'
then 'Blue Red'
else NULL end [Function]
from DataTable d
Please assist.
Thank you,
Monique
October 13, 2014 at 3:26 am
I just want to add that I want to create the function as the data in the DataTable can be loads of combinations, so using a Case Statement does not make sense.
October 13, 2014 at 4:06 am
When you call this function, will you always pass exactly 2 arguments (eg 1 2), or do you want to pass a parameter list with a variable list of params?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 13, 2014 at 4:16 am
Hi
No, the amount will always differ.
Example 1 or 1 2 or 1 1 1 2
October 13, 2014 at 4:28 am
So, I want it to work like this
FunctionName (1) = Red
FunctionName (1 2) = Red Green
FunctionName (1 1 1 2) = Red Red Red Green
October 13, 2014 at 5:02 am
Here is a solution which relies on Jeff Moden's infamous '8k Splitter'[/url].
It assumes that the DelimitedSplit8K function has been created in the database you are going to call it from.
if object_Id('tempdb..#colour', 'U') is not null
drop table #colour
create table #colour
(
Id int primary key,
Descr varchar(50)
)
insert #colour
(Id, Descr)
values
(1, 'Red'),
(2, 'Green'),
(3, 'Blue')
--select * from #colour c
declare @LookupVal varchar(1000) = '1 3 3 3 2 3'
declare @RetVal varchar(max) = ''
select
@RetVal = @RetVal + c.Descr + ' '
from
dbo.DelimitedSplit8K(@LookupVal, ' ') split
join #colour c on c.Id = split.item
order by
split.ItemNumber
select
@RetVal
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 13, 2014 at 6:02 am
Hi
Thank you so much! You are awesome!
Can I just ask one more thing?
I've added ', ' to my query [@RetVal = @RetVal + c.Descr + ', '] because I want the data to be separated by a comma. I want it to look like this:
Function (1 2 3) = [Red, Green, Blue]
But it comes out looking like this (it adds an extra comma to the end):
Function (1 2 3) = [Red, Green, Blue,]
I don't want a comma to be added to the end of the row. How can this exception be put into the function so that I do not have to remove the comma in my SQL query?
Please assist this last time 🙂
October 13, 2014 at 7:50 am
monique 88355 (10/13/2014)
HiThank you so much! You are awesome!
Can I just ask one more thing?
I've added ', ' to my query [@RetVal = @RetVal + c.Descr + ', '] because I want the data to be separated by a comma. I want it to look like this:
Function (1 2 3) = [Red, Green, Blue]
But it comes out looking like this (it adds an extra comma to the end):
Function (1 2 3) = [Red, Green, Blue,]
I don't want a comma to be added to the end of the row. How can this exception be put into the function so that I do not have to remove the comma in my SQL query?
Please assist this last time 🙂
I recommend that you post the whole function so that nothing gets broken in the process of helping you figure this out.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 13, 2014 at 10:36 am
Instead of SELECT @RetVal in my above example,
select left(@RetVal,len(@Retval)-1)
Returns everything apart from the rightmost character. This may be what you need ...
--Edit: fixed inconsistent formatting
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 15, 2014 at 10:58 pm
Thanx for you assistance.
Works perfectly.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply