November 14, 2012 at 8:32 pm
Hi experts,
For example: Col A Varchar(6) contains FGHIJK
Col B char(1)
I need to insert the first character - F in this case, into Column B
I need update a column in all rows by retrieving the left-most character of Col A and insert that value into Column B
How can this be done without getting the "would be truncated" error?
Thanks, Bill
November 14, 2012 at 8:52 pm
Is this what you need ? Look up the LEFT function in BOL Books On Line
CREATE TABLE #T(Cola VARCHAR(6),Colb CHAR(1))
INSERT INTO #T(Cola,colb)
SELECT 'FGHIJK',LEFT('FGHIJK',1)
-- check result
SELECT Cola,colb FROM #T
Result:
Colacolb
FGHIJKF
November 14, 2012 at 8:54 pm
Ah, I got it.
Update Table1
Set ColB = (Select Substring(ColA, 1, 1));
This worked.
Thanks.
November 14, 2012 at 8:59 pm
If it is always going to the first character of column a, might consider redefining column b as a computed column to save you the pain of ongoing updates, etc. depending on your needs of course.
Just a thought.
November 14, 2012 at 9:02 pm
Thanks for the tip, Jeff. I'll look into computed columns - never used them. Yes always position 1.
November 14, 2012 at 10:06 pm
That is a very good tip from Jeff.....Computed Columns are not used a lot....they are even sometimes avoided in the cases they could be used....it may be due to negligence or ignorance.....the following links would give you a very good insight at Computed Columns :
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply