Click here to Skip to main content
15,849,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I Have a table
20140411002 LD0000240301 003045
20140411005 LD0000350301 003074
20140411007 LD0000250301 003049
20140411008 LD0000390301 003073
20140411009 LD0000300301 003058

And i have to change it to

20140411-04-002 LD0000240301 003045
20140411-04-005 LD0000350301 003074
20140411-04-007 LD0000250301 003049
20140411-04-008 LD0000390301 003073
20140411-04-009 LD0000300301 003058

loop through your table column ID and add '-04-' like below using STUFF
STUFF ( ID, 9, 0, '-04-' )

STUFF ( character_expression , start , length , replaceWith_expression )

The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
Share this answer
Maciej Los 29-Oct-14 6:28am    
Dilan Shaminda 29-Oct-14 6:36am    
Thank you :-)
phanithly 9-Nov-14 20:29pm    
Thank you so much It work 100%
Dilan Shaminda 9-Nov-14 21:19pm    
you are welcome :-)
Then Update your table with help of Substring

Try it ,
create table #dummy(ID nvarchar(max), LDID nvarchar(max), CusDI nvarchar(max))

insert into #dummy values('20140411002', 'LD0000240301' ,'003045')

insert into #dummy values('20140411005 ', 'LD0000240301' ,'003074')
insert into #dummy values('20140411007 ', 'LD0000240301' ,'003049')
insert into #dummy values('20140411008 ', 'LD0000240301' ,'003073')
insert into #dummy values('20140411009', 'LD0000240301' ,'003058')
select *From #dummy
update B set,0,9)+'-04-'+SUBSTRING(a.ID,9,3) from #dummy  as a inner join #dummy as b on a.CusDI=b.CusDI
select *From #dummy
Share this answer
Maciej Los 29-Oct-14 6:28am    
King Fisher 29-Oct-14 6:58am    
Thank you Boss :)
phanithly 9-Nov-14 20:31pm    
Thank you ! finally it work
Use simple UPDATE[^] statement, like:
UPDATE TableName SET ID = LEFT(ID, 8) + '-04-' + RIGHT(ID, LEN(ID)-8)

For further information, please see:
Share this answer
King Fisher 29-Oct-14 7:03am    
wow. you did Simply :)
Maciej Los 29-Oct-14 10:00am    
Thank you, King_Fisher ;)
King Fisher 30-Oct-14 4:08am    
Assist me :)
create table table_name(ID nvarchar(max),LDID nvarchar(max),CusDI nvarchar(max));

insert into table_name(ID,LDID,CusDI)values('20140411002','LD0000240301','003045');  
insert into table_name(ID,LDID,CusDI)values('20140411005','LD0000350301','003074'); 
insert into table_name(ID,LDID,CusDI)values('20140411007','LD0000250301','003049'); 
insert into table_name(ID,LDID,CusDI)values('20140411008','LD0000390301','003073'); 
insert into table_name(ID,LDID,CusDI)values('20140411009','LD0000300301','003058');

SELECT cast(substring(replace(ID,',',''),0,len(replace(ID,',',''))-2) as nvarchar)+'-04-'+cast(substring(replace(ID,',',''),9,len(replace(ID,',',''))-2) as nvarchar) as ID,LDID,CusDI from table_name
Share this answer
You can directly update your table for column ID as below

Update table set ID=Replace(ID,'2014041100','20140411-04-00')

if the preceding character are same for given column.
Share this answer
King Fisher 29-Oct-14 8:07am    
hard-coding ,Op have Sample records ,ID may be '20140411002'..
20140411010-20140411099 this case you can't use this as need to change the Query for Every Conditions.
Shweta N Mishra 29-Oct-14 8:17am    
correct this can b applied only if preceding characters are same. Otherwise
using Stuff,substring and Left , right function are the better options.
King Fisher 29-Oct-14 8:20am    
phanithly 9-Nov-14 20:34pm    
:) it work but if i have more records. so i will hard-coding right ?
Shweta N Mishra 10-Nov-14 3:34am    
Try the 1st solution, select STUFF ( ID, 9, 0, '-04-' ) by Dilan. That would be optimal zoluion in your case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900