| Author |
Topic |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 15:56:48
|
| I have a table like this:ID BegNo EndNO1 1 52 10 20I want to output like this:ID BegNo EndNo Remarks1 1 5 NULL2 10 20 6-9pls use the follwing script:update bset b.remarks = case when b.begno>(a.endno+1) then (a.endno+1) +'-'+ (b.begno-1)when b.begno<(a.endno+1) then NULLendfrom table_1 a join table_1 b on a.id+1 = b.id |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 16:04:52
|
| [code]Update bset b.remarks = Case when b.begno>(a.endno+1) then Convert(varchar(10),(a.endno+1)) +'-'+ Convert(varchar(10,(b.begno-1))Else NULLendfrom table_1 a Join table_1 b on a.id+1 = b.id[/code] |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 16:18:27
|
| if i have like this:Id BegNo EndNo1 0001 00052 0010 0020Out put looks like :Id Remarks:1 006-009How do i get hole no too |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 16:22:13
|
| Didn't get it? |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 16:23:40
|
| no i jut get 6-9 it suppose to be 0006-0009 |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 16:28:00
|
| My BegNo and EndNo are also varcharso i tried this:but not getting resultUpdate bset b.remarks = Case when Convert(varchar(50),b.begno)>convert(varchar(50),(a.endno+1)) then b.Col2+Convert(varchar(50),(a.endno+1)) +'-'+ b.Col2+Convert(varchar(50),(b.begno-1))Else NULLendfrom table_1 a Join table_1 b on a.id+1 = b.id |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 16:45:54
|
| How do i filter only 000 from begno and endno? If i can filter 000, i can add 000 on new updated filed |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 16:53:26
|
| Why you keep on changing requirement? Also why is it stored in varchar? |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 17:02:57
|
| no no i am not changing my reqirment because i need 000 too so.i tried to convert like thiscast(substring(begno,1,10) as varchar(10)) |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 17:28:01
|
Sodeep, did u get how to get 000 to on updated field.I want :Id Remarks:1 006-009quote: Originally posted by sodeep
Update bset b.remarks = Case when b.begno>(a.endno+1) then Convert(varchar(10),(a.endno+1)) +'-'+ Convert(varchar(10,(b.begno-1))Else NULLendfrom table_1 a Join table_1 b on a.id+1 = b.id
|
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 17:46:16
|
| any body has script to display number on 3 digit format like : 003, 030, 300the above script i need to display on 3 digit format |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-02-19 : 18:08:33
|
| Try this.DECLARE @t TABLE (begno varchar(50),endno varchar(50), remarks varchar(50), id int)insert @tselect '1','5',null,'1' union allselect '10','20',null,'2'Update bset b.remarks = Case when b.begno>(a.endno+1) then Right('000' + Convert(varchar(10),(a.endno+1)),3) +'-'+ Right('000' + Convert(varchar(10),(b.begno-1)),3)Else NULLendfrom @t a Join @t b on a.id+1 = b.idselect * from @tgives1 5 NULL 110 20 006-009 2hope this is what you need. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 18:13:23
|
| [code]Update bSet b.remarks = Case when b.begno>(a.endno+1) then (Case len('000'+ Convert(varchar(10),(a.endno+1))) When 5 then Substring('000'+ Convert(varchar(10),(a.endno+1)),2,4) + '-' + Substring('000'+ Convert(varchar(10),(b.begno-1)),2,4)When 6 thenSubstring('000'+ Convert(varchar(10),(a.endno+1)),3,4) + '-' + Substring('000' + Convert(varchar(10),(b.begno-1)),3,4)when 7 thenSubstring('000'+ Convert(varchar(10),(a.endno+1)),4,4) + '-' + Substring('000' + Convert(varchar(10),(b.begno-1)),4,4) Else '000'+ Convert(varchar(10),(a.endno+1))+'-'+'000' + Convert(varchar(10),(b.begno-1) )End)Else NULLendfrom table_1 a INNER JOIN table_1 b on a.id+1 = b.id[/code] |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 18:22:14
|
| great jobthanks |
 |
|
|
|