Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Updating comma delimited field

Author  Topic 

Umar001
Starting Member

10 Posts

Posted - 2012-05-22 : 02:13:23
have a table in which there are comma delimited fields, I want to update them based on conditions
if my additional_procedure location field contains null or not 1 and 2 i need to make it 1

I am using following method
1) spiliting into rows
2) checking those values where ever there is null, not 1 and not 2, set it to 1
3) again making those rows into comma delimited field
4) I want to put that new comma delimited field into table


here is temp table

[code="sql"] drop Table #temp
Create Table #temp
(MRN nchar(10) null,
additional_procedure varchar(max),
additional_procedure_Location varchar(max))
insert into #temp
(MRN,additional_procedure,additional_procedure_Location)
values('1','9772800,9773100,9773200', '1,2,3'),
('2','9773300, 9773400, 9773500, 9773600', '2,1,,1'),
('3','9774300,9774400,9774500,9774600','1,2,2,4'),
('4','9775300,9775400,9776100','4,,2'),
('5','9776500,9776800,9776900,9777100','2,1,,1')

Select * from #temp




I wrote 2 table valued functions first one to spilit them into rows and than another to find out if there is null or not 1 and not 2 and for setting value to 1

I also made a procedure in which I am passing two parameters and getting the new comnplete field in comma delimited format , however I am unable toput this new comma delimited field into the table and need help how to update it, your help would be highly appreciated


my functions are as below

First Function to spilit these comma delimited fields into rows
[code="sql"]
GO

/****** Object: UserDefinedFunction [dbo].[Validate_2_DelimitedFields_Id] Script Date: 05/22/2012 15:27:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE FUNCTION [dbo].[Validate_2_DelimitedFields_Id]
(
@delimited1 nvarchar(max),
@delimited2 nvarchar(max),
@delimiter1 nvarchar(100),
@delimiter2 nvarchar(100),
@StartFieldNum int,
@EndFieldNum int
) RETURNS @t TABLE
(
id int,
val1 nvarchar(max),
val2 nvarchar(max)
)

AS
BEGIN

insert into @t(id,val1,val2)
select a.id,a.val,b.val from [HealthValidator].[dbo].[ValidateDelimitedField] (@delimited1,',',1,50) as a full outer join
[HealthValidator].[dbo].[ValidateDelimitedField] (@delimited2,',',1,50) as b on a.Id = b.Id

RETURN
END



GO


[/code]


Second funtion to find where is not 1 not 2 and null values and setting it to 1
[code="sql"]CREATE FUNCTION [dbo].[UpdateProcedureLocation]
(
-- Add the parameters for the function here
@ap varchar(max),
@apl varchar(max)

)
RETURNS
@ReturnTable TABLE
(
-- Add the column definitions for the TABLE variable here
[id] int NULL,
[ap] [varchar](max) NULL,
[apl] [varchar](max) NULL

)
AS
BEGIN
INSERT INTO @ReturnTable(id,ap,apl)
Select * from dbo.Validate_2_DelimitedFields_id(@ap, @apl ,',',',',1,26)

Update @ReturnTable
Set apl = '1'
where Id in (Select id from dbo.Validate_2_DelimitedFields_id(@ap, @apl ,',',',',1,26)
where ((nullif(val1,'') is not null ) and nullif(val2,'') is null) or
(nullif(val1,'') is not null ) and (val2 <> '2' and val2 <> '1'))

RETURN
END[/code]


and third procedure in which I am passing these two and getting a new field in the same comma delimited format


[code="sql"]
create proc [dbo].[Sp_UpdateProcedureLocation]
@ap varchar(Max),
@apl varchar(Max),
@newapl varchar(Max) output
as

declare
@TEMPTable TABLE (id int, ap varchar(20), apl varchar(20))
Insert into @TEMPTable(id,ap,apl)

Select * from dbo.[UpdateProcedureLocation](@ap, @apl)

DECLARE @NewProcLoc VARCHAR(MAX)
SELECT @NewProcLoc = COALESCE(@NewProcLoc+',' ,'') + apl
FROM @temptable

SELECT @NewProcLoc
Set @newapl = @NewProcLoc


GO[/code]




My required output is

[code="sql"] drop Table #temp
Create Table #temp
(MRN nchar(10) null,
additional_procedure varchar(max),
additional_p
[/code]rocedure_Location varchar(max))
insert into #temp
(MRN,additional_procedure,additional_procedure_Location)
values('1','9772800,9773100,9773200', '1,2,1'),
('2','9773300, 9773400, 9773500, 9773600', '2,1,1,1'),
('3','9774300,9774400,9774500,9774600','1,2,2,1'),
('4','9775300,9775400,9776100','1,1,2'),
('5','9776500,9776800,9776900,9777100','2,1,1,1')

Select * from #temp
[/code]

Umar Memon

influent
Constraint Violating Yak Guru

367 Posts

Posted - 2012-05-22 : 15:50:37
Is there a reason to store them all in one field rather than having a row for each and leaving it up to the application to create the comma-separated list?
Go to Top of Page
   

- Advertisement -