473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to have strings automatically truncated?

"String or binary data would be truncated"
Can MS SQL automatically do the truncation?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Mar 1 '06 #1
6 7601

John Bokma wrote:
"String or binary data would be truncated"
Can MS SQL automatically do the truncation?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html


Sometimes.

Mar 1 '06 #2
John Bokma (jo**@castleamber.com) writes:
"String or binary data would be truncated"
Can MS SQL automatically do the truncation?


Yes, if you issue the command SET ANSI_WARNINGS OFF.

However, this is not really recommendable, as there are functionality
in SQL Server that requires ANSI_WARNINGS to be on.

So you are better off by truncating the data yourself, for instance
with cast or convert. Or bounce thata over a variable. SQL Server does
not complain if you assign a variable with an overlong value.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 1 '06 #3
Erland Sommarskog <es****@sommarskog.se> wrote:
John Bokma (jo**@castleamber.com) writes:
"String or binary data would be truncated"
Can MS SQL automatically do the truncation?


Yes, if you issue the command SET ANSI_WARNINGS OFF.

However, this is not really recommendable, as there are functionality
in SQL Server that requires ANSI_WARNINGS to be on.

So you are better off by truncating the data yourself, for instance
with cast or convert. Or bounce thata over a variable. SQL Server does
not complain if you assign a variable with an overlong value.


Thanks. Is there an easy way to insert and truncate the data via SQL in
one go (or give something like an "It's ok to truncate hint"? I don't want
to hardcode the max length of a text column (for example) in the program
code.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Mar 2 '06 #4
John Bokma (jo**@castleamber.com) writes:
Erland Sommarskog <es****@sommarskog.se> wrote:
John Bokma (jo**@castleamber.com) writes:
"String or binary data would be truncated"
Can MS SQL automatically do the truncation?


Yes, if you issue the command SET ANSI_WARNINGS OFF.

However, this is not really recommendable, as there are functionality
in SQL Server that requires ANSI_WARNINGS to be on.

So you are better off by truncating the data yourself, for instance
with cast or convert. Or bounce thata over a variable. SQL Server does
not complain if you assign a variable with an overlong value.


Thanks. Is there an easy way to insert and truncate the data via SQL in
one go (or give something like an "It's ok to truncate hint"? I don't want
to hardcode the max length of a text column (for example) in the program
code.


See above about SET ANSI_WARNINGS. That's the hint you are looking for.
But also beware of the caveat.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 2 '06 #5
Erland Sommarskog <es****@sommarskog.se> wrote:
John Bokma (jo**@castleamber.com) writes:
Erland Sommarskog <es****@sommarskog.se> wrote:
John Bokma (jo**@castleamber.com) writes:
"String or binary data would be truncated"
Can MS SQL automatically do the truncation?

Yes, if you issue the command SET ANSI_WARNINGS OFF.

However, this is not really recommendable, as there are
functionality in SQL Server that requires ANSI_WARNINGS to be on.

So you are better off by truncating the data yourself, for instance
with cast or convert. Or bounce thata over a variable. SQL Server
does not complain if you assign a variable with an overlong value.


Thanks. Is there an easy way to insert and truncate the data via SQL
in one go (or give something like an "It's ok to truncate hint"? I
don't want to hardcode the max length of a text column (for example)
in the program code.


See above about SET ANSI_WARNINGS. That's the hint you are looking
for. But also beware of the caveat.


I was somehow thinking on SET ANSI_WARNINGS OFF, query, back on, but
indeed, need to read the problems this might cause. I was hoping that
there would be an easier option (well, maybe it's easy enough :-) )

Thanks,

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Mar 2 '06 #6
John Bokma (jo**@castleamber.com) writes:
I was somehow thinking on SET ANSI_WARNINGS OFF, query, back on, but
indeed, need to read the problems this might cause. I was hoping that
there would be an easier option (well, maybe it's easy enough :-) )


More specifically, these are the issues you can run into:

1) You cannot perform updates that would affect an indexed view or an
indexed computed column.
2) SELECT queries cannot make use of index on views and computed columns
and my run slower.
3) Cannot perform queries on linked server.
4) Adding SET ANSI_WARNINGS ON in a stored procedure causes the procedure
to be recompiled, which can degrade performance if compilation cost
is high in relation to execution time.
5) Overflows and division by zero will go unnoticed, leading to other
stange and unexpected results. This can mitigated by setting ARTITHABOT
ON. (Which has to be on for indexed views and indexed computed columns
anyway.)

My experience is that if you use stored procedure or parameterised
statments is that you rarely run into this errors. This is because,
the errors is only raised on column assignment, but not parameter or
variable assignment.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
3
by: Petr Prikryl | last post by:
Hi all, My question is: How do you tackle with mixing Unicode and non-Unicode parts of your application? Context: ======== The PEP 3000 says "Make all strings be Unicode, and have a...
3
by: Sandra-24 | last post by:
I'd love to know why calling ''.join() on a list of encoded strings automatically results in converting to the default encoding. First of all, it's undocumented, so If I didn't have non-ascii...
3
by: GinnyP | last post by:
Hi, When my database opens I have it set up to run a bunch or queries, format, generate, and save output as excel files. Can I set this to run automatically at night? If yes how would I do this? ...
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
1
by: lloyd | last post by:
Hello, Over the past 24 hours or so, all of my Python-List e-mails have been truncated to subject list only. No posts. Are others experiencing this problem? Or is it just on my end? Thanks,...
2
by: jalil.feghhi | last post by:
I have an access database that has a field (type memo) that keeps very long strings. When the value of this field is very long, my Visual Basic 6.0 code does not return the whole value and...
1
by: esurkes | last post by:
I have been able to create a web browser control in a form that properly opens a website based on an address from another form. Now I am trying to figure out how to set the corresponding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.