Solved

Is there a way to rewrite an INSERT INTO ...  SELECT clause in which a target field is smaller than the target field ?

Posted on 2009-05-04
3
328 Views
Last Modified: 2013-12-05
I am developing an application using SQL Server 2000 as the back end database.

Is there a way to rewrite an INSERT INTO ...  SELECT clause in which the target table dbo.LOCATION_ALL has a CITY field defined as VARCHAR 15
while the source table dbo.LOCATION_ALL_MOD has a CITY field defined as VARCHAR 50 ?

If I can avoid doing so, I don't want to change the field lengths for the field CITY.

I currently get the error:
Server: Msg 8152, Level 16, State 9, Line 3
String or binary data would be truncated.
The statement has been terminated.


 


INSERT INTO dbo.LOCATION_ALL
      ([Location_ID], [Description], [Address1], [Address2], [City], [State], [Zip], [CostCenter],
            [PropertyStatus], [LeasedSpace], [LeaseStatus], [EndDate])
SELECT [LocationID], [Description], [Address1], [Address2], [City], [State], [Zip], [Cost Center],
            [Property Status], [LeasedSpace], [Lease Status], [EndDate]
FROM dbo.LOCATION_ALL_MOD
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 6

Expert Comment

by:bokist
ID: 24297698
Try this

NSERT INTO dbo.LOCATION_ALL
      ([Location_ID], [Description], [Address1], [Address2], [City], [State], [Zip], [CostCenter],
            [PropertyStatus], [LeasedSpace], [LeaseStatus], [EndDate])
SELECT [LocationID], [Description], [Address1], [Address2], left([City],1,15), [State], [Zip], [Cost Center],
            [Property Status], [LeasedSpace], [Lease Status], [EndDate]
FROM dbo.LOCATION_ALL_MOD
0
 
LVL 2

Expert Comment

by:alfil_28
ID: 24297719
Hi
Maybe using al sql functin might help.
Have you tried using the
LEFT ( character_expression , integer_expression )
on the select clause that might give the size you are looking for.

Hope this helps
0
 
LVL 6

Accepted Solution

by:
bokist earned 500 total points
ID: 24297780
Oops, small mistake in LEFT function
This is right way :
INSERT INTO dbo.LOCATION_ALL
      ([Location_ID], [Description], [Address1], [Address2], [City], [State], [Zip], [CostCenter],
            [PropertyStatus], [LeasedSpace], [LeaseStatus], [EndDate])
SELECT [LocationID], [Description], [Address1], [Address2], left([City],15), [State], [Zip], [Cost Center],
            [Property Status], [LeasedSpace], [Lease Status], [EndDate]
FROM dbo.LOCATION_ALL_MOD

you can use also substring function :

INSERT INTO dbo.LOCATION_ALL
      ([Location_ID], [Description], [Address1], [Address2], [City], [State], [Zip], [CostCenter],
            [PropertyStatus], [LeasedSpace], [LeaseStatus], [EndDate])
SELECT [LocationID], [Description], [Address1], [Address2], substring([City],1,15), [State], [Zip], [Cost Center],
            [Property Status], [LeasedSpace], [Lease Status], [EndDate]
FROM dbo.LOCATION_ALL_MOD



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

867 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

19 Experts available now in Live!

Get 1:1 Help Now