We have a database hosted on our SQL Server 2016 server that has email content stored in an IMAGE type column. I am trying to convert the data to its string representation. Everything I've read says that I should be able to just use CONVERT(VARCHAR(MAX), Field, 1) but that's not working. I've tried the following as a test:
declare #body varbinary(max);
select #body = substring(cast(body as varbinary(max)),1,13) from dbo.arc_bodies where id = -2147467502
select #body, convert(varchar(1000), #body, 1), convert(varchar(1000), 0x3C68746D6C3E)
So I take the first 13 bytes (just for testing I kept the data short instead of grabbing the whole column) from the "body" IMAGE field and convert it to VARBINARY. In the last statement, I show the contents of #body, try the convert, and try the convert by just copying the raw data.
This is what I end up with:
(No column name) (No column name) (No column name)
0x3C68746D6C3E0D0A3C68656164 0x3C68746D6C3E0D0A3C68656164 <html>
The convert of the #body returns the exact same data as the raw binary but the convert of the raw data works. What am I doing wrong?
In 3rd column in the last query you don't use any style. Just do the same for the 2nd one:
select #body, convert(varchar(1000), #body), convert(varchar(1000), 0x3C68746D6C3E)
A reference to MS docs
In SQL Server 2005 I am trying to query a varchar(MAX) column which has some rows with text data that exceed the 8192. Yet, In Management Studio I have under Tools --> Options --> Query Results --> Results to Text --> Max numbers of characters displayed in each column = 8192, which is a maximum. Accordingly, it seems the truncation on these rows occurs only due to the limitation imposed by text output.
The only thing I see to get around this is to use a SUBSTRING function to grab say the first 8000 chars, then the next 8000 chars etc. etc. But this is ugly and error prone.
I should mention that SSIS and BCP are not options for me.
Does anyone have a better suggestion? Thanks!
You can export the data to a flat file which will not be truncated. To do this:
Right click the Database
Click Tasks -> Export Data
Select your Data Source (defaults should be fine)
Choose "Flat File Destination" for the Destination type.
Pick a file name for the output.
On the "Specify Table Copy or Query", choose "Write a query to specify the data to transfer"
Paste in your query
Remaining steps should be self explanatory. This will output the file to text and you can open it in your favorite text editor.
I also use XML but a slightly different method that gets around most of the issues with XML entitisation.
declare #VeryLongText nvarchar(max) = '';
SELECT top 100 #VeryLongText = #VeryLongText + '
' + OBJECT_DEFINITION(object_id)
FROM sys.all_objects
WHERE type='P' and is_ms_shipped=1
SELECT LEN(#VeryLongText)
SELECT #VeryLongText AS [processing-instruction(x)] FOR XML PATH('')
PRINT #VeryLongText /*WILL be truncated*/
Make sure that the "XML data" limit in SSMS is set sufficiently high!
Did you try this simple solution? Only 2 clicks away!
At the query window,
set query options to "Results to Grid", run your query
Right click on the results tab at the grid corner, save results as any files
You will get all the text you want to see in the file!!! I can see 130,556 characters for my result of a varchar(MAX) field
My solution was a bit round-about but got me there (as long as the output is less than 65535 characters):
In SQL Management Studio, set the limit for grid results to 65535 (Tools > Options > Query Results > SQL Server > Results to Grid > Non XML data)
Run the query, output to grid
Right-click the results, choose "Save Results As..." and save the results to a file
Open the file in notepad or similar to get the output
UPDATE: To demonstrate that this works, here's some SQL that selects a single 100,000 character column. If I save the grid output to a csv file, all 100,000 characters are there with no truncation.
DECLARE #test nvarchar(MAX), #i int, #line nvarchar(100)
SET #test = ''; SET #i = 100
WHILE #i < 100000
BEGIN
SET #test = #test + STUFF(REPLICATE('_', 98) + CHAR(13) + CHAR(10), 1, LEN(CAST(#i AS nvarchar)), CAST(#i AS nvarchar))
SET #i = #i + 100
END
SELECT #test
Notes:
It doesn't seem to make any difference what the character length setting is, as I orignally thought.
I'm using SQL 2008 R2 (both the server and Management Studio)
It doesn't seem to make a difference if the long column is stored in a local variable (as in this example), or selected from an actual table
I ran in to this trying to export XML. This is the solution I used:
Select the Result to Grid option, right click the link that shows up in the Results pane, then select Save Results As, choose the All Files file type, give the file a name and click Save. All the xml data is saved correctly to a file.
I'm using SSMS 10, and I could't get Torre's solution to work. The export wizard kept thinking the input column was an image:
The data type for "input column "XML_F52E2B61-18A1-11d1-B105-00805F49916B" (26)" is DT_IMAGE
In SSMS if you select data from a row it is limited to a small number of characters, but if you Edit data from a row, the full value will be there. It might not always be there but if you ctrl-a, ctrl-c then past it in an editor it will all be there.
If given a choice I would have the query return the data as "For XML Auto" or "For XML Raw" or "For XML explicit" that way the limitations are much higher and you can do much more with the outputed results.
I usually use XML to get huge debug string as output (using test harness from Luke):
declare #test nvarchar(max), #i int, #line nvarchar(100)
set #test = ''; set #i = 100
while #i < 100000
begin
set #test = #test + stuff(replicate('_', 98) + char(13) + char(10), 1, len(cast(#i as nvarchar)), cast(#i as nvarchar))
set #i = #i + 100
end
-- ctrl+d for "results to grid" then click the xml output
--select cast('<root>' + #test + '</root>' as xml)
-- revised
select #test for xml path(''), type;
Another workaround , use HeidiSql for this tricky queries. It does not have the limits in the field lenght.
The truncation you are talking about only happens in Management Studio. If you pull the column into another app, it will not be truncated.
There's no way you're using Query Analyzer to talk to SQL Server 2005. Do you mean Management Studio?
I have this code
SELECT
TRY_CONVERT(varchar(150), "PCR_Fecha") AS "PCR_Fecha"
FROM OPENQUERY(EXTRACCION, 'SELECT * FROM EXTRACCION.Extraccion')
But i get the error
Error converting data type DBTYPE_DBTIMESTAMP to datetime2.
I know there are wrong values in PCR_Fecha (like 40218:00:00 or 14mayo09) that's why I'm trying to convert them to varchar.
I can see the data using
SELECT * FROM OPENQUERY(EXTRACCION, 'SELECT CAST(PCR_Fecha AS varchar(26)) FROM EXTRACCION.Extraccion');
The linked server is from Filemaker and there PCR_Fecha is set as a date.
Looking for an answer I found that I could define "dbtimestamp_rules=2" in connection string but I don't know how to add the rule.
Any other suggestions?
I found that I could change the data type from Filemaker and set it to text (it was date before) and that solves the problem.
With sql it was
SELECT
Convert(NVARCHAR(26),PCR_Fecha)
FROM OPENQUERY(EXTRACCION, 'SELECT CAST(PCR_Fecha AS VARCHAR(26)) as PCR_Fecha FROM EXTRACCION.Extraccion')
A cast and then convert, anyways thx :)
I have a column in Excel I'm trying to import, and it includes codes like the following:
01166
1166
2354654765432
xx132
I use IMEX=1 and everything imports as TEXT but when I assign the select to temporary table with nvarchar type, the codes with long numbers become incorrect:
1001051 becomes 1.00105e+006
I tried to cast to bigint, but this makes the code lose the leading 0 in 01166
this is my current query:
INSERT INTO #XTEMP
SELECT DISTINCT
(CASE ISNUMERIC([item_code]) WHEN 1 THEN CAST(CAST([item_code] AS BIGINT) AS nvarchar) ELSE CAST([item_code] AS nvarchar) END)
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;IMEX=1;Database=C:\path\file.xls',
'SELECT * FROM [sheet$]')
IMEX=1 allows reading mixed columns; it does not force data values to be text.
Looking at this SO page, you can see a possible workaround if you set HDR=NO, but that has its own complications.
If you can, the best thing to do is set the format of the column in the spreadsheet to Text.
Not exactly a fix, but more like a work around; I wrote an Excel Macro that appended a character (e.g. 'B') to the codes, then removed it when importing using Openrowset like:
select RIGHT([code], LEN([code])-1)
I want the user to be able to choose from a dropdown Combobox listing some of the fields of the database, then below enter a search term and all then all the results that meet the query be displayed in the dbgrid. I'm not sure how to link the current value of the ComboBox into the sql statement. I tried using this
begin
with ADOQuery do begin
Close;
SQL.Clear;
SQL.Add ('SELECT * FROM List WHERE combobox1.text =' + QuotedStr (Asearchterm.Text));
Open;
And it doesn't work. The error i'm getting is "The parameter combobox1.text has no default value". Any ideas?
If you're wanting to use the combobox1 text value as part of the sql statement, you'd set up the sql string along the lines of
'SELECT * FROM List WHERE [' + combobox1.text + '] = ''' + QuotedStr(Asearchterm.Text) + ''''
is probably what you're looking for. I added the extra quotes around the QuotedStr because I'm guessing that the filter is not always going to be numeric values. This will work for numeric as well as non-numeric values.
instead of 'combobox1.text' you have to put actual column name that you are looking for. And you can use LIKE keyword and some wildcards. something like:
SELECT * FROM 'table WHERE 'column' LIKE '%YOUR_SEARCH_TEXT%';
That was for a search...if you want to find exact string then you have to use = operator instead of LIKE
More info here