April 19, 2008 at 9:40 am
Hi,
I have one column with values something like this "A1704", "A2734", "CC404" and so on. I want to copy these values to another column with the extension ".html", the resulting column will look something like this "A1704.html", "A2734.html", "CC404.html"
Thanks for any help.
April 19, 2008 at 2:21 pm
For starters, start with something like Select val + '.html' from mytable
I'll leave it as an exercise to the reader to figure out how to UPDATE a column.
April 20, 2008 at 1:49 pm
Thanks Jeremy for your tip. I already did by using Update, but the problem was that in resulting value it adds space between two values something like this "ABC .html". Then I use LTRIM(RTRIM(@string)) which solved my problem.
Thanks
April 20, 2008 at 3:36 pm
Thanks for the feedback. However we could have helped you more if you actually stated what your real problem was...
🙂
April 20, 2008 at 4:51 pm
It would appear that the value for the "val" column is incorrect thoughout the table. It should not have trailing spaces. I'd recommend finding the root cause for that and fixing it as well as doing a cleanup on the table to repair existing problems.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 20, 2008 at 7:43 pm
Yes Jeff you are right. I am having this problem in all of my tables. All the values have extra spaces on the right. You can easily see this by selecting the column values in Management Studio. When you select any value it give blue strip bigger then the physical value.
I will check if I found any solution for this.
Thanks anyway.
April 20, 2008 at 9:19 pm
is your data type char or varchar? If the string you are holding in a char type column is less than the number defined, it will automatically be padded with spaces. varchar will not.
April 20, 2008 at 9:27 pm
Are the columns defined as CHAR() instead of VARCHAR()? Depending on system settings, that could be the problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 21, 2008 at 10:40 am
I wonder about why one would want to append a constant ".html" to a particular column and store the result in the database. For any larger number of rows, it may be more effiecient to allow the application to see xxxxx.html by using a view that takes care of the concatenation.
April 21, 2008 at 11:36 am
john.arnott (4/21/2008)
I wonder about why one would want to append a constant ".html" to a particular column and store the result in the database. For any larger number of rows, it may be more effiecient to allow the application to see xxxxx.html by using a view that takes care of the concatenation.
John,
You are correct in one way. If this field is only used in only a couple of spots in the application, SP's, or jobs, then I would agree with you that it would be better to assume the '.html' to all results from this field after trimming the value.
However, if you use this field in many multiple locations, that would mean you would have to add the code to annex '.html' cleanly while trimming the value from the field each time. That could get tedious after a while. To get around that, you would annex the '.html' in the field so that you only have to do it once.
Now there is one exception to this. In OO programing, you could have your class take care of this for you. You would just have to remember to apply this class to this field each time you use it.
There may be a few other explanations for one way or the other, but these are the ones that come to my mind quickly.
Dave Novak
April 21, 2008 at 6:06 pm
Calculated column would take care of it nicely...
--Jeff Moden
Change is inevitable... Change for the better is not.
April 22, 2008 at 2:52 pm
Hi everyone,
Back to my problem. I am using VARCHAR() as my data type. Non of the column values holf CHAR().
Thanks
April 22, 2008 at 6:49 pm
Then, you need to trace the source to the root cause.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 28, 2008 at 6:37 am
Jeff Moden (4/20/2008)
Are the columns defined as CHAR() instead of VARCHAR()? Depending on system settings, that could be the problem.
I think Jeff might have been talking about the SET ANSI_PADDING statement
BOL 2005:
ANSI_PADDING = ON --> Trailing blanks in character values inserted into varchar columns are not trimmed. Trailing zeros in binary values inserted into varbinary columns are not trimmed. Values are not padded to the length of the column
ANSI_PADDING = OFF --> Trailing blanks in character values inserted into a varchar column are trimmed. Trailing zeros in binary values inserted into a varbinary column are trimmed.
This might mean that your ANSI_PADDING was on and your application sent extra blanks in the SQL.
Or that it was CHAR and someone decided to modify the column to VARCHAR...
April 28, 2008 at 7:15 am
Zactly...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply