| Author |
Topic |
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-03-01 : 10:39:51
|
| Dear All,I want to update a field for all rows in a table with default value ’LIST1’,’LIST2’ and ‘LIST3’ in following wayFor eg:FOI1334816 01/03/2012 Mr sharon Peckham LIST1TOI3454563 07/12/2012 Mr elizabeth goddard LIST2RTY8383831 12/06/2012 Mr Natalie LIST3DoorFOI1334816 01/03/2012 Mr sharon Peckham LIST1TOI3454563 07/12/2012 Mr elizabeth goddard LIST2RTY8383831 12/06/2012 Mr Natalie LIST3......How to write update statement for this? Any help will be highly appreciatedThanksRegards,SG |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-01 : 10:43:22
|
| [code]UPDATE tSET 3rdfield= CASE WHEN 1stfield LIKE 'FOI%' THEN 'LIST1' WHEN 1stfield LIKE 'TOI%' THEN 'LIST2' WHEN 1stfield LIKE 'RTY%' THEN 'LIST3' ENDFROM table[/code]make sure you replace fields with correct names------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-03-01 : 11:01:16
|
| Dear visakhThanks for your reply.Your query is based on field value ie LIKE 'FOI%' or LIKE 'TOI%'But the value need not to be like this.Just for ex i mentioned this.i just want to update First row field with 'list1'second row field with ' list2'third row field with 'list 3'4 th with 'list1'5th row with 'list2'and so one..Hope you understand.Sorry if my explanation is not clear.ThanksSG |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-01 : 11:26:10
|
quote: Originally posted by satheesh Dear visakhThanks for your reply.Your query is based on field value ie LIKE 'FOI%' or LIKE 'TOI%'But the value need not to be like this.Just for ex i mentioned this.i just want to update First row field with 'list1'second row field with ' list2'third row field with 'list 3'4 th with 'list1'5th row with 'list2'and so one..Hope you understand.Sorry if my explanation is not clear.ThanksSG
when you say 1st 2nd etc on what basics you're ordering it?there's no concept of ordering in a table unless you specify it by means of ORDER BY------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-03-01 : 11:33:19
|
| Dear X002548I am producing this record for email campaign.I need to order the data by expirydate either by desc/ascThen i need to produce the 'list1' by reterving the data in the row 1,4,7,10 etcsecond list - row 2,5,8,11 etcthrid list - row 3,6,9,12 etcI created a post 'Dividing the select query result' both for same purpose onlySorry my explanation is not clear.Thanks |
 |
|
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-03-01 : 11:36:32
|
| Dear Visakh,>>when you say 1st 2nd etc on what basics you're ordering it?there's no concept of ordering in a table unless you specify it by means of ORDER BY<<Yes Visakh.i am trying to Update the row by ordering by either ASC or descThanksRegards,SG |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-01 : 13:45:57
|
| [code]UPDATE tSET 3rdfield= CASE WHEN (Rn-1)%3=0 THEN 'LIST1' WHEN (Rn-1)%3=1 THEN 'LIST2' WHEN (Rn-1)%3=2 THEN 'LIST3' ENDFROM (SELECT ROW_NUMBER() OVER (ORDER BY expirydate ASC) AS Rn,* FROM table)t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|