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.
| Author |
Topic |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2006-08-28 : 23:41:13
|
| Table : ColumnA ColumnBABCDEFGH1234567811223344X55667788ZZ77882212236667I want to move 4 character from third position in ColumnA to ColumnB ColumnA ColumnBABCDEFGH-------CDEF 12345678-------345611223344X------223355667788ZZ-----66777788221--------8822223------------36667-----------67How can i do ?Thank you very much ! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-28 : 23:50:03
|
[code]update tableset ColumnB = substring(ColumnA, 3, 4)[/code] KH |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2006-08-29 : 00:10:13
|
quote: Originally posted by khtan
update tableset ColumnB = substring(ColumnA, 3, 4) KH
What happen if ColumnA is null or data is empty ? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 00:17:43
|
"What happen if ColumnA is null or data is empty ?"Why don't you give it a try and see what is the result ? KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 00:22:16
|
Here it goes..declare @table table( ColumnA varchar(100), ColumnB varchar(4))insert into @table (ColumnA)select 'ABCDEFGH' union allselect '12345678' union allselect '11223344X' union allselect '55667788ZZ' union allselect '7788221' union allselect '223' union allselect '6667' union allselect '' union allselect NULLupdate @tableset ColumnB = substring(ColumnA, 3, 4)select * from @table/*ColumnA ColumnB -------------- ------- ABCDEFGH CDEF12345678 345611223344X 223355667788ZZ 66777788221 8822223 36667 67 NULL NULL(9 row(s) affected)*/ KH |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2006-08-29 : 00:27:23
|
quote: Originally posted by khtan "What happen if ColumnA is null or data is empty ?"Why don't you give it a try and see what is the result ? KH
Thank khtan very much !Sorry for my question , because when i write code (not sql) i also use substring,it occur exception when data is empty.Sorry for any inconvinent ! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 00:48:54
|
no problem  KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 01:29:21
|
And to close the MOVE operation, useupdate tableset ColumnA = STUFF(ColumnA, 3, 4, '') Otherwise, it is just a COPY operation.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|