Stanlyn said:
It does not hurt to trim a trimmed value
Yes it does, you've seen that. Just because alltrim(field) is a simple expression with only one field. VFP does not deduct the field type to be the same as the field, it dedcts C as type and tries to fit this into a char field, which fails for anything longer than 254 characters. You're causing that problem with alltrim.
So using alltrim in SQL is really a bad habit, if not an error.
If you have trimmed values you don't alltrim them. It hurts, not because it changes the value, but it changes the type from memo to char. If that's not sunken in by now, I can't help you, but you need to change your habit.
Here's another reason alltrim is really bad, even though it doesn't error in this case:
Code:
Create Cursor demo (cText Memo)
Insert into demo values ('hello')
Insert into demo values ('world!')
Select Alltrim(cText) as cText from demo into cursor result
? Len(cText)
Go 2
? cText
To determine the field data type of a result, VFP uses the result of the first record. And if that's a short string, that makes the result short and all strings in further records are trimmed silent without error.
If you don't see it let me point out the exclamation point of 'world!' is lost in the result cursor. The result field is char(5) because 'hello' has length 5.
This is really awful to use. The only thing that saves you in case the original field type is char, is that VFP then uses that field length and doesn't trim, so the same example done with field type char(10) goes without error:
Code:
Create Cursor demo (cText char(10))
Insert into demo values ('hello')
Insert into demo values ('world!')
Select Alltrim(cText) as cText from demo into cursor result
? Len(cText)
Go 2
? cText
So that may already have saved you a lot of times. It doesn't, if the original field type is memo, though. Other databases won't determine field data types the way VFP does. But you always risk cutting off data. BEsides, when VFP keeps the field type char(10) in such cases, what effect remains from the alltrim? None, the values are trimmed and put back into char(10) fields, which pads them again, with spaces. So you never win with alltrim, no matter if the original field type is memo or char.
So the only place it makes sense to alltrim is when you transfer the cursor field to the user interface. In VFP controls it makes no sense as using controlsource alltrim(field) will make that control readonly. In case you generate HTML that's fine to do, though.
But don't alltrim in the SQL. Never ever.
In VFP forms you could set the controlsource to a field and in the base class of your textboxes and editboxes you could have This.value = alltrim(this.value) in gotfocus. Something like that would trim values just in time, when needed.
And then, it's still more appropriate to do the REPLACE ALL description with alltrim(description) as that's done within the cursor and doesn't change field types as a query into a second cursor does. Afterward, even if the MSSQL data was "dirty" with trailing spaces, your VFP side data has that trimmed away.
Chriss