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

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.

Related

Exporting data with NTEXT columns to Excel in a regularly basis (as Job)

Initial situation:
I'm stuck in a simple task (in my opinion it should be simple...)
I have a collection of data which should be exported weekly to Excel.
This export contains 104 columns, from which 57 are nvarchar(max) and contains item descriptions and other information in different languages for our sales guys.
The report will have something around 2000 to 8000 rows.
I use a SQL Server 2017 CU 16
My intention:
I intended to do an SSI Job with an Excel template where the columns are predefined (width, data type and so on).
This job would have something like those steps:
Delete existing Excel file
copy Excel template as a new Excel file
DataFlowTask using SQL Server as the source and Excel destination as the target
What I already tried:
If I use the excel template with only headers, I get the following error for each of the nvarchar(max) columns:
[Excel Destination [2]] Error: An error occurred while setting up a
binding for the "ColumnName" column. The binding status was
"DT_NTEXT".
When I prepare the template having it prefilled with one row. This row has a long text (more than 255 characters) for the columns where nvarchar(max) is in the source, everything runs fine but, this dummy line is still existing.
Another try I did was dropping the sheet using an "Execute SQL Task" to the Excel File Connection and recreating the sheet using a create table statement in another "Execute SQL Task" to the same Excel file connection, I get the same error as above. Although I'm using NTEXT as the datatype for the relevant columns.
Question:
How can I export data seamlessly into a preformatted excel file which contains NTEXT?
Thank you very much in advance for any assistance.

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

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

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

copy table from one SQL server 2005 to another SQL Server 2005 table

I wish to copy a single table where a particular row has a value of '76'. I then need to copy this table to another server in a separate domain.
I tried to use the export tool, but I can't restrict it to rows that have a value of 76 only
What's the best way to go about it?
Right Click your database:
Your server and database is already selected.
The easiest way if its not a regular thing then you could probably create a file.
Lets say abc.txt on your desktop.
Choose the flat file destination and then select the file.abc.txt in this case.
Format your wish. I like to use delimited and text qualifier " and since there are no column names and no data in our file uncheck the column names in the first row. Next select write a query to specify the data to transfer.
Lets assume you have a table TblUsers with columns username, password, value.
your query will be:
Select * from tblUsers where value = '76'
Next (Make changes if you wish, I like to leave the defaults) > Click edit mappings > Next > Finish!
Then go to your destination server and database and then do almost the same thing but import.
Thats it!

SQL Server 2005 -> Excel export doesn't keep data types?

Trying (and largely succeeding) to export the results of a query from SQL Server to Excel, like so:
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\exported excel files\exported_data.xls;',
'SELECT * FROM [Query$]') SELECT dbo.blabbityblah FROM dbo.the_table
It works! Sort of. It does export the data to the excel file, but it puts it all in there as text, even though some of the columns are datetime and most of them are numbers. None of them are being convert()-ed in the query itself. I've tried preformatting the cells in the actual Excel file before running the query, but it ignores the existing formating and spits it all out as text again.
There's got to be a way to do this, right?
excel dont have data type, its text based and preformat not work becus it replace existing file. if u want datatype try MS Access.
Look into using a schema.ini file to define the datatypes in a csv or txt. when you open either in excel you may achieve what you want
[sample_out.csv]
Format=CSVDelimited
DecimalSymbol=.
Col1=DATE datetime
Col2=FName Text
Another approach you may want to look at depending on your needs is to use the import and export wizard. You can customize a query for the data and specify the data type in the wizard. If you are using a SKU other than Express you can the run it right away or save the SSIS package is generated for further manipulation.