Solved

Appending/removing values with comma (,)

Posted on 2006-06-28
5
348 Views
Last Modified: 2012-08-14
I have a field called, DevPref, on a table (Admin_Priv), and the field has values like below:

default,splits,installments
default,splits
default,splits
default,installments,splits
NULL

I need to update the field with adding and removing "sortasc" or "sortdesc".

for instance,

adding "sortasc" will be:  i.e. default,splits,installments,sortasc
updating this one will be: default,splits,installments,sortdesc

for the NULL row
it would simply be:
                       sortasc
updating this one will be:
                       sortdesc

When I append "sortasc" or "sortdesc" to the tail so that the existing values (i.e. default,splits,installments) won't be impacted.

0
Comment
Question by:ethanjohnsons
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 17002804
Your update statement will look like this:

UPDATE Admin_Priv
SET DevPref = ISNULL(DevPref + ',', '') + 'sortasc'

To update sortasc to sortdesc:

UPDATE Admin_Priv
SET DevPref = REPLACE(DevPref, 'sortasc', 'sortdesc')
0
 
LVL 10

Assisted Solution

by:imrancs
imrancs earned 100 total points
ID: 17002815
First create this function
---------------


--This UDF returns string values

CREATE FUNCTION dbo.GetValuesFromListS
(
      @ValuesList varchar(8000)
)
RETURNS
@ParsedList table
(
      Value Varchar(1000)
)
AS
BEGIN
      DECLARE @Value varchar(1000), @Pos int

      SET @ValuesList = LTRIM(RTRIM(@ValuesList))+ ','
      SET @Pos = CHARINDEX(',', @ValuesList, 1)

      IF REPLACE(@ValuesList, ',', '') <> ''
      BEGIN
            WHILE @Pos > 0
            BEGIN
                  SET @Value = LTRIM(RTRIM(LEFT(@ValuesList, @Pos - 1)))
                  IF @Value <> ''
                  BEGIN
                        INSERT INTO @ParsedList (Value)
                        VALUES (@Value) --Use Appropriate conversion
                  END
                  SET @ValuesList = RIGHT(@ValuesList, LEN(@ValuesList) - @Pos)
                  SET @Pos = CHARINDEX(',', @ValuesList, 1)

            END
      END      
      RETURN
END
---------------

1. Append a value
Declare @NewValue Varchar(100)
Select @NewValue = 'sortasc'

Update Admin_Priv Set DevPref = IsNull(DevPref, '') = @NewValue
Where ID_Col = <SomeValue>

2. Removing a value
Declare @ValueToRemove Varchar(100)
Declare @OldValues Varchar(1000)
Declare @NewValues Varchar(1000)

Select @ValueToRemove = 'sorasc'
Select @OldValue = ''
Select @NewValue = ''

Select @OldValues = DevPref From Admin_Priv Where ID_Col = <SomeValue>
Select @NewValues = Coalesce(@NewValues, '') + Value + ',' From dbo.GetValuesFromListS(@OldValues) Where Value <> @ValueToRemove

If @NewValues <> ''
  Select @NewValues = Left(@NewValues, Len(@NewValues)-1)

Update Admin_Priv Set DevPref =  @NewValues  
Where ID_Col = <SomeValue>


Imran
0
 

Author Comment

by:ethanjohnsons
ID: 17003069
UPDATE Admin_Priv
SET DevPref = ISNULL(DevPref + ',', '') + 'sortasc'

To update sortasc to sortdesc:

UPDATE Admin_Priv
SET DevPref = REPLACE(DevPref, 'sortasc', 'sortdesc')

This works fine, but I need to make sure that ONLY one of 'sortasc' or 'sortdesc' is added.


UPDATE Admin_Priv
SET DevPref = ISNULL(DevPref + ',', '') + 'sortasc'

adds mutiple of 'sortasc', so, if 'sortasc' or 'sortdesc' exists, it has to skip adding.
0
 
LVL 28

Accepted Solution

by:
rafrancisco earned 400 total points
ID: 17003115
Just put a WHERE clause to make sure that sortasc is not in the string yet:

UPDATE Admin_Priv
SET DevPref = ISNULL(DevPref + ',', '') + 'sortasc'
WHERE DevPref IS NOT LIKE '%sortasc%'
0
 

Author Comment

by:ethanjohnsons
ID: 17024717
that will work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now