Solved

Appending/removing values with comma (,)

Posted on 2006-06-28
5
351 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Replication question 9 41
SqlServer no dupes 25 34
SQL Update trigger 5 16
TSQL - How to declare table name 26 28
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

813 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

9 Experts available now in Live!

Get 1:1 Help Now