Sunday, June 12, 2011

SQL 필드의 데이터를 여러개로 나누어 저장하기

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=10178

This should work:

UPDATE myTable SET Fname=SubString(Name, 1, CharIndex('*', Name)-1),
MName=SubString(Name, CharIndex('*', Name)+1, CharIndex('*', Name, CharIndex('*', Name)+1) - CharIndex('*', Name)-1),
Lname=SubString(Name, CharIndex('*', Name, CharIndex('*', Name)+1)+1, DataLength(Name))
WHERE FName Is Null


You may want to put Ltrim() around each SubString() call to remove leading spaces. I'd recommend performing this update in batches, maybe 100,000 rows each, to prevent your transaction log from overflowing. You can do that by adding SET ROWCOUNT before the UPDATE statement:

SET ROWCOUNT 100000
UPDATE myTable SET Fname=SubString(Name, 1, CharIndex('*', Name)-1),
MName=SubString(Name, CharIndex('*', Name)+1, CharIndex('*', Name, CharIndex('*', Name)+1) - CharIndex('*', Name)-1),
Lname=SubString(Name, CharIndex('*', Name, CharIndex('*', Name)+1)+1, DataLength(Name))
WHERE FName Is Null


And just keep running this update until 0 rows are updated. You may also need to truncate the transaction log between each call.

No comments:

Post a Comment