Solved

string clipping in dynamic sql

Posted on 2008-06-17
6
207 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 69

Accepted Solution

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

Expert Comment

by:ScottPletcher
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:ScottPletcher
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now