SSMS - Importing into nvarchar(max) still giving truncate errors - sql

I'm using SSIS to import an Excel table into SQL Server.
The field in the SQL Server table is set as nvarchar(max) but it still gives me Truncate Error.
The column that I want to import can have any number of characters, it could be 1 or it could be 10,000. It's a free-text filed without any limitations.

Go into the Advanced settings of your Excel Source Component, and manually set the length of the Output columns.

SSIS samples your data to get an idea of each column. It will use the max length of the sample to determine the "proper" field size. Of course this causes constant issues.
Can you add something to order your data to make the longest first?
ORDER BY LEN(LongFIELD) DESC
Check out StackExchange for more info:
Text was truncated or one or more characters had no match in the target code page When importing from Excel file

Related

Exporting SQL Server table containing a large text column

I have to export a table from a SQL Server, the table contains a column that has a large text content with the maximum length of the text going up to 100,000 characters.
When I use Excel as an export destination, I find out that the length of this text is capped and truncated to 32,765.
Is there an export format that preserves the length?
Note:
I will eventually be importing this data into another SQL Server
The destination SQL Server is in another network, so linked servers and other local options are not feasible
I don't have access to the actual server, so generating back up is difficult
As is documented in the Excel specifications and limits the maximum characters that can be stored in a single Excel cell is 32,767 characters; hence why your data is being truncated.
You might be better off exporting to a CSV, however, note that Quote Identified CSV files aren't supported within bcp/BULK INSERT until SQL Server 2019 (currently in preview). You can use a characters like || to denote a field delimited, however, if you have any line breaks you'll need to choose a different row delimitor too. SSIS, and other ETL tools, however, do support quote identified CSV files; so you can use something like that.
Otherwise, if you need to export such long values and want to use Excel as much as you can (which I actually personally don't recommend due to those awful ACE drivers), I would suggest exporting the (n)varchar(MAX) values to something else, like a text file, and naming each file with the value of your Primary Key included. Then, when you import the data back you can retrieve the (n)varchar(MAX) value again from each individual file.
The .sql is the best format for sql table. Is the native format for sql table, with that, you haven't to concert the export.

How to resolve SSIS Package Truncation error

SSIS Error: 0xC02020A1
Tying to import data to SQL 2008 from CSV file, I am getting below error.
> Error: 0xC02020A1 at Data Flow Task, Source – Distribution by xyz
> table from CSV [1]: Data conversion failed.
> The data conversion for column "ID" returned status value 4 and
> status text "Text was truncated or one or more characters had no match
> in the target code page.".
previous have used varchar and never a problem, I have tried to convert data to Int and even increased the size but still getting this error. I have also tried using the Advance editor and changed data to almost anything I could think would cover datatype on that column, still getting an error. Thanks for the advice
Most likely you have "bad" records in your raw file.
For "bad", if could be one of these two: 1) implicitly conversion cannot be done to the string value; 2) string is too large (exceed 8000).
For debugging this, change the destination column to VARCHAR(MAX).
Then load the raw file (do not forget to increase the external column length to 8000 in the Advanced page in flag file connection manager).
if:
1) it is loaded successfully, query the table where ISNUMERIC([that numeric column]) =0, if anything returned, that is the bad record that cannot be converted when loading.
2) it is not loaded correctly, try to see if you have any value from that field has more than 8000 characters. (could use C# script if it is impossible to manually check)

How to export VARCHAR2 data that includes commas(!) to Excel from MSSQLMgmtStudio(2012)

First, a grumble: MS builds SQL Server Studio AND Excel, but can't make one save in the standard format of the other?
OK, I'm a data analyst, but not allowed to change/mod either the data or structures directly. So full READ, but no WRITE.
I'm trying to do a dump so I can do some of this analysis offline, as I have no remote access either.
So one VARCHAR2 column in this table is for comments on the purchase of the asset being described/tracked. Of course, there are commas. The only export types built into SQL Server Studio are .csv and .txt, and .csv just turns into a mess when 'comma' is included as a delimiter.
So after an hour or so of screwing around with this, (including reading a thread on methods for excluding the one column from a SELECT while still exporting the other 221 columns in the table, without having to write them all out manually (fun reading, impressive, but means I'd have to figure out which of them actually works, and then still export the one column separately and insert it in the Excel separately)) I am throwing this problem on the pile at StackOverflow.
Someone else must have worked around this frustration of the .csv format as export VS the commas embedded in 'comment' text.
Any help would be appreciated.
Why don't you simply select all data in ssms result window, then copy and then paste in a blank excel file?
It should copy paste all data in correct format including comma valued fields in single column.
Try that.
So If you replace the ' to some special character you can export it.
Select
Replace(columnName,'''','`')
from Table
Other solution if you use the manager studio
https://learn.microsoft.com/en-us/sql/integration-services/import-export-data/start-the-sql-server-import-and-export-wizard

How can I extract the value of a varbinary(max) column?

I have a varbinary(max) column that is storing images in an SQL database.
I am working on a newdb script, where an application creates a new instance of the db and populates a few of the tables. One of those tables I am working on is initializing that image column.
In order to do this, I printed the contents of the column using a select statement and pasted the content into the insert statement of the newdb script. This appeared to work initially, but the image didn't load correctly.
So I compared the DATALENTH() of the original data (5469988) and the new data (21839). It appears the Microsoft SQL Server management Studio - 2014 cut off the data why I copied it from the original db at a certain point. I need to be able to get the entire content of the column. Any ideas?
select cast(convert(varchar(max), VarBinaryMaxColumn, 1) as xml) from Table
Instead of copying/pasting, right-click on the results and do 'Save Results As...', and that should export the full contents. Funny thing is setting the query output to text or file explicitly will still truncate long data values.
If you copy and paste your limited to the query result options. Mostly columns will be cut of after a certain lenght (often 256 chars).
You can select in the top bar "save result as..." which will prompt you an dialog for data export.
You can use the data export wizard too.

Importing Excel File into SQL Server 2012

I'm having the hardest time importing an Excel file with approx 300 records into SQL server without getting errors. I am getting an error that text will be truncated, but I checked and the max character count in that field is 520 characters. That should be fine importing into a Varchar(MAX) field. Any recommendations on best procedures for importing Excel files (just one tab) into SQL Server 2012? I have tried everything I can think of including editing mappings, etc, and still cannot get it to properly import.
The truncation error is in the import 'source' settings. In the mappings screen choose 'Edit Mappings', then you can adjust the field size. There's also an 'Edit SQL' on the mapping screen where you can wrap your fields and cast them to the correct size
select cast(col1 as varchar(50))...
Consider exporting the data to csv and then do the import. I always have formatting issues with Excel, especially with dates.