mssql ntext conversion is failing - sql-server

I have old data that also contains an ntext column.
I want to convert it to nvarchar. When I try something like
Select Charindex(Cast(old_column as nvarchar(max))) it say it cuts off data.
Researching I found that nvarchar(max) should be able to store more than ntext
How can I search information inside of ntext? Or how can I convert the information?

Related

SSIS Convert m/dd/yyyy to yyyymmdd with inconsistencies

I'm loading many files into a SQL SERVER database. I have one flat file that has a Date Column coming in as string[DT_STR].
I have TWO "date fields" in my database. One is varchar, one is datetime.
Converting the datetime column is no issue, I just use Data Conversion/Derived Column if necessary. However, this varchar column is giving me trouble. Our database values for this column should be in yyyymmdd format. However, on this single file the format of the dates change.
Normally I'd do a SUBSTRING(...) expression here, but the difficulty is that the format of these dates change. some examples of values could be
08/16/2017
8/16/2017
08/6/2017
08/06/2017
10/6/2017
10/06/2017
This makes the challenge harder. I tried LEN([DATE]) == NUM_HERE ? do_THING : OTHER_CALC, but this approach failed because the length of 10/6/2017 is the same as 8/06/2017 which will give me the wrong result. Does anyone have a good workaround for this?
Perhaps a simple convert to date and then into the final format. If 2012+, use try_convert() to trap any bogus dates.
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('08/16/2017')
,('8/16/2017')
,('08/6/2017')
,('08/06/2017')
,('10/6/2017')
,('10/06/2017')
Select *
,Formatted = convert(varchar(8),convert(Date,SomeCol),112)
from #YourTable
Returns
SomeCol Formatted
08/16/2017 20170816
8/16/2017 20170816
08/6/2017 20170806
08/06/2017 20170806
10/6/2017 20171006
10/06/2017 20171006
Convert the varchar data to datetime and convert that to a formatted string
SELECT CONVERT(varchar,(CONVERT(datetime, '8/6/2017')),112)

SQL Server - data lost when converted datetime to varchar

I had to restore a table that contained a datetime column.
I used bulk insert to insert the data from a CSV file. However the import couldn't insert the datetime values because SQL server saw it as a different format.
I ended up modifying the table, removing the datetime data type and replacing it with a varchar.
The issue is the data got converted from this format: 7/15/2015 3:41:57 PM to something like this: 47:47.0
Is there a way I can convert these values back or is the data lost?
As #ChrisSteele mentioned, your data is hosed. It likely got this way by Excel's cool feature to convert datetime strings to integers. Try re-saving the original file in notepad, or changing the format of the column from Date/Datetime to Text if you're using Excel.

How to show CLOB type in a SELECT in SQL Server?

I have a table with one column of CLOB type data, they are all very short no more than 20 bytes, however I cannot see the actual string in the CLOB data.
For example if I use SELECT *, under the CLOB type every data is like:
CLOB, 8 Bytes
CLOB, 15 Bytes
CLOB, 9 Bytes
But I just want to see the content of the CLOB data.
I tried:
SELECT DBMS_LOB.SUBSTR(ClobColumnName, 20 ,1)
And it doesn't work, error is:
Error Code: 4121, SQL State: S1000
Cannot find either column "DBMS_LOB" or the user-defined function or aggregate "DBMS_LOB.SUBSTR", or the name is ambiguous.
So can I ask what's the syntax for direct display a CLOB data in a query?
I'm using SQL Server with dbVisualizer.
I figured out one solution. There should be better ways, please show more possible solutions in the comments.
SELECT CAST(ClobColumnName AS VARCHAR(50)) AS ClobColumnName ;
I have table with one column has CLOB data type(1000K), after storing message/data into CLOB column and found one solution see the actual data in CLOB column.
SELECT CAST(T.CLOB_COLUMNNAME AS VARCHAR(1000)) AS SAMPLEDATA
FROM TABLE_NAME AS T
The above query CAST the CLOB(Character Large Objects) into a normal String.
To see it in DbVis you just have to change it in the options.
There is an entry for the display of CLOB columns.
I presume you are using jDTS driver to connect to the SQL Server.
In the driver properties of the connection you can set the "USELOBS" to False to automatically cast them to string.
I had the same problem and solved it by using DBeaver (http://dbeaver.jkiss.org/) instead of dbVisualizer.
When I use DBeaver and do a select * from my SQLServer I can just double-click the CLOB in the result set and it opens in a new window with the content. Very slick.

Converting DBCLOB/CLOB to XML

We are facing a problem in converting/casting DBCLOB to XML.
Background
We are storing some xml data in a column of type DBCLOB (1073741823). For one of our requirements, we have to convert this data to XML type so that we can take advantage of Xquery to filter the result. For doing this conversion, we are using the following SQL query to convert DBCLOB to XML data type.
SELECT XMLCAST (XMLPARSE (DOCUMENT (CAST (CAST (COLUMN1 AS DBCLOB(32672)) AS VARCHAR (32672)))) AS XML from TABLE1
Problem
For some scenario the size of data in DBCLOB column is more than 32672
and, since we are converting DBCLOB to XML via VARCHAR, so the output
get limited to 32672, and XML conversion fails.
What would be the way to achieve this casting (clob to xml)
Thanks in advance
Actually i was casting it to varchar as XMLPARSE function was expecting a string expression.
After going through the documentation again, i converted it to blob and then to XML. It worked, the sample query which worked is given below for reference.
SELECT
XMLCAST (
XMLPARSE (
DOCUMENT CAST (
COLUMN1 AS BLOB
)
PRESERVE WHITESPACE
) as XML
)
FROM
TABLE1
Thanks for support
Any casting you perform in the database will be contstrianed by the limits of the data types you're using (e.g. varchar 32672 KB in your example).
Try using XMLSERIALIZE instead.

SQL Server - trying to convert column to XML fails

I'm in the process of importing data from a legacy MySQL database into SQL Server 2005.
I have one table in particular that's causing me grief. I've imported it from MySQL using a linked server and the MySQL ODBC driver, and I end up with this:
Col Name Datatype MaxLen
OrderItem_ID bigint 8
PDM_Structure_ID int 4
LastModifiedDate datetime 8
LastModifiedUser varchar 20
CreationDate datetime 8
CreationUser varchar 20
XMLData text -1
OrderHeader_ID bigint 8
Contract_Action varchar 10
ContractItem int 4
My main focus is on the XMLData column - I need to clean it up and make it so that I can convert it to an XML datatype to use XQuery on it.
So I set the table option "large data out of row" to 1:
EXEC sp_tableoption 'OrderItem', 'large value types out of row', 1
and then I go ahead and convert XMLData to VARCHAR(MAX) and do some cleanup of the XML stored in that field. All fine so far.
But when I now try to convert that column to XML datatype:
ALTER TABLE dbo.OrderItem
ALTER COLUMN XMLData XML
I get this message here:
Msg 511, Level 16, State 1, Line 1
Cannot create a row of size 8077 which
is greater than the allowable maximum
row size of 8060. The statement has
been terminated.
which is rather surprising, seeing that the columns besides the XMLData only make up roughly 90 bytes, and I specifically instructed SQL Server to store all "large data" off-row....
So why on earth does SQL Server refuse to convert that column to XML data??? Any ideas?? Thoughts?? Things I can check / change in my approach??
Update: I don't know what changed, but on a second attempt to import the raw data from MySQL into SQL Server, I was successfully able to convert that NTEXT -> VARCHAR(MAX) column to XML in the end..... odd..... anyhoo - works now - thanks guys for all your input and recommendations! Highly appreciated !
If you have sufficient storage space, you could try selecting from the VARCHAR(MAX) version of the table into a new table with the same schema but with XMLData set up as XML - either using SELECT INTO or by explicitly creating the table before you begin.
PS - it's a side issue unrelated to your problem, but you might want to check that you're not losing Unicode characters in the original MySQL XMLData field by this conversion since the text/varchar data types won't support them.
Can you ADD a new column of type xml?
If so, add the new xml column, update the table to set the new column equal to the XmlData column and then drop the XmlData column.
Edit
I have a table "TestTable" with a "nvarchar(max)" column.
select * from sys.tables where name = 'TestTable'
This gives a result containing:
[lob_data_space_id] [text_in_row_limit] [large_value_types_out_of_row]
1 0 0
yet I can happily save 500k characters in my nvarchar(max) field.
What do you get if you query sys.tables for your OrderItems table?
If your [text_in_row_limit] is not zero, try this, which should convert any existing in-row strings into BLOBs:
exec sp_tableoption 'OrderItems', 'text in row', 0
and then try to switch from nvarchar(max) to xml.
From BOL,
Disabling the text in row option or
reducing the limit of the option will
require the conversion of all BLOBs;
therefore, the process can be long,
depending on the number of BLOB
strings that must be converted. The
table is locked during the conversion
process.

Resources