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
 General SQL Server Forums
 New to SQL Server Programming
 String queery in SQL

Author  Topic 

dardar
Starting Member

10 Posts

Posted - 2012-12-02 : 03:40:11
Hi guys,
new to sql and there is somethi gi can't figure out how to do.

i got a column that has a value such as "1,2,3,4" (4 numbers, could be any numbers) or "1,2,3,4,5" (5 numbers)

i need to loop my entire table (lets say it's a single column) and check if the value in the current column has 4 numbers (the first case) than i need to add another number to the end, and if it has 5 numbers do nothing.

can it be done?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-02 : 03:41:43
will 4 numbers always be in the same order always? or can they come in any order?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dardar
Starting Member

10 Posts

Posted - 2012-12-02 : 03:58:19
any order. the only thing i'm sure about is that there will be 4 ',' in the string (or 5 if it has 5 numbers)
i thought to count ',' but not sure how :(
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-02 : 04:01:25
ok for counting ,'s this is enough

SELECT CASE WHEN LEN(Field) - LEN(REPLACE(Field,',','')) = 4 THEN Field
WHEN LEN(Field) - LEN(REPLACE(Field,',','')) = 3 THEN Field + @YourOtherNo
END
FROM table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dardar
Starting Member

10 Posts

Posted - 2012-12-02 : 05:34:59
ty! got it to work.....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-02 : 07:03:19
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -