Solved

string clipping in dynamic sql

Posted on 2008-06-17
6
212 Views
Last Modified: 2010-03-20
    UPDATE RecHead SET ShortRecipeName = RecipeName

I am calling a MSSQL driver dynamically with this sql statement. I am getting error 'string or binary data would be truncated'
I assume I must clip the RecipeName string to overcome this error but can't seem to find function that accomplishes this.

Any suggestions appreciated.
0
Comment
Question by:JoeSnyderJr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 35 total points
ID: 21805087
use the LEFT function to cut the last digits off

  UPDATE RecHead SET ShortRecipeName = LEFT(RecipeName, 30)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21805088
in the example I gave, it takes the first 30 characters.
0
 
LVL 11

Assisted Solution

by:dready
dready earned 40 total points
ID: 21805090
If you have a RecipeName and ShortRecipename, it seems strange to me to put the long version in the shortName field;
BUt if you just want to cut it off, you could do something like:

UPDATE RecHead SET ShortRecipeName = Substring(RecipeName, 0, LEN)

where you should replace LEN with the length of the shortRecipeName field.

~dready
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 50 total points
ID: 21805853
UPDATE RecHead
SET ShortRecipeName = LEFT(RecipeName, LEN(ShortRecipeName))
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 21806068
D'OH, I meant to lookup the length of the actual column and put that in there.


DECLARE @ShortLength INT
SELECT @ShortLength = CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'dbo'
    AND table_name = 'tablename'

UPDATE RecHead
SET ShortRecipeName = LEFT(RecipeName, @ShortLength)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 21806237
DOUBLE D'OH:
I knew there was a function for that but forgot it temporarily ... then I remembered :-) :


UPDATE RecHead
SET ShortRecipeName = LEFT(RecipeName, COL_LENGTH ('RecHead' , 'ShortRecipeName'))
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question