SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 String queery in SQL
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dardar
Starting Member

Israel
10 Posts

Posted - 12/02/2012 :  03:40:11  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 12/02/2012 :  03:41:43  Show Profile  Reply with Quote
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

Israel
10 Posts

Posted - 12/02/2012 :  03:58:19  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 12/02/2012 :  04:01:25  Show Profile  Reply with Quote
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

Israel
10 Posts

Posted - 12/02/2012 :  05:34:59  Show Profile  Reply with Quote
ty! got it to work.....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/02/2012 :  07:03:19  Show Profile  Reply with Quote
wc

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000