Converting DBCLOB/CLOB to XML - database

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.

Related

mssql ntext conversion is failing

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?

SQL Server : convert Varchar to XML - then operate on it and select data?

I got a Varchar column which holds XML. I would like to convert them to JSON so I did a quick test.
Here is what worked so far:
DECLARE #xml XML
SET #XML = '<content>
<value>123456</value>
</content>'
SELECT
'{"foo":"' + b.value('(./value)[1]', 'VARCHAR(50)') + '"}'
FROM
#xml.nodes('/content') AS a(b)
Result:
{"foo":"123456"}
So far so good, but this does not do half the job that has to be done.
I have lots of rows, and I want to convert all rows at once.
I have multiple values inside a content tag and therefore I cannot just add a json prefix and suffix since inside that array there will be more than one ID.
I read this article : https://learn.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type?view=sql-server-ver15 but since my target column is a VARCHAR and has to be cast, it actually didn't help me...
So first I cast the values to xml:
SELECT CAST(mytextField.TEXTVALUE AS XML) AS xmlData
FROM myDataTable
WHERE mytextFieldId = 12345
This returns the values as XML:
<content><value>12345</value></content>
<content><value>9874</value></content>
<content><value>Foo</value><value>Bar</value></content>
Now I'm stuck.
How can I select this result in a new query?
What would be the best approach to convert this to json format?
In the first snippet I added a prefix and a suffix to the value of the xmls value tag. How could I do this with the cast rows I selected?

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 Converting varchar to datetime

I got a problem in SQL Server with converting a varchar to datetime. I would like to convert/update whole column [datelog] in table:
[dd.mm.yyyy hh:mm:ss]` to `[yyyy-mm-dd hh:mm:ss]
In SQL Server 2012+ you can use PARSE or TRY_PARSE to parse a text value according to a specific culture.
Assuming your text follows the German culture ('de-DE') you can parse it to datetime with :
select PARSE('24.11.2015 13:10:55' as datetime using 'de-DE')
eg:
select PARSE(datelog as datetime using 'de-DE')
The real solution though would be to use the correct field type, ie datetime. It's almost guaranteed that someone, somewhere will either enter text with the wrong format or try to convert the text using the wrong culture.
Date types on the other hand, have no format, they are simply binary values. Using them is faster, safer and easier.
Tricky solution,
DECLARE #inputDate AS VARCHAR(20)='21.11.2015 06:59:00' -- [dd.mm.yyyy hh:mm:ss]
SET #inputDate = REPLACE(#inputDate ,'.' ,'/')
SELECT CONVERT(VARCHAR(24) ,CONVERT(DATETIME ,#inputDate ,103) ,121) OutputDate -- [yyyy-mm-dd hh:mm:ss]
Still you need to change as per your table columns.
temp table with one column of type varchar
create table #temp3 (someDate varchar (30))
insert into #temp3 values ('23.03.1989 15:23:43')
using a combination of concat, substring and right
select concat
(
SUBSTRING(someDate,7,4),'-', SUBSTRING(someDate,4,2),'-',SUBSTRING(someDate,1,2), ' ', right(someDate, 8)
)
from #temp3
gives: 1989-03-23 15:23:43

Cast XML to money

I have some XML data that I need to add up as currency. Every conversion I try gets the error
Explicit conversion from data type xml to ____ is not allowed.
Here's a simple example, containing several failed attempts.
DECLARE #xmlstuff xml
SET #xmlstuff = '3'
SELECT
CAST (#xmlstuff AS varchar(80)) as worksButDontCare,
--CAST (#xmlstuff AS MONEY) as dollars, -- Explicit conversion from data type xml to money is not allowed.
--CAST (#xmlstuff AS NUMERIC) as number, -- Explicit conversion from data type xml to numeric is not allowed.
--CONVERT (decimal(18,2),#xmlstuff ) AS cd1_feeAmount, -- Explicit conversion from data type xml to decimal is not allowed.
The answer that I found is to convert to varchar as an intermediate step.
CAST ((CAST (#xmlstuff AS varchar(80))) AS MONEY) AS xmlToVarcharToMoney
TSQL does not allow for direct conversion from xml to any numeric type.
So, as #incircuitous pointed, you will have to convert first to text, and then to Money
For more information about TSQL data type conversion, look for the big table on MSDN.
The conversion table in MSDN is indeed useful for reference, +1 for #GabrielRainha.
To get value out of XML data type I would recommend to look into XML specific methods, namely value() for this particular case :
DECLARE #xmlstuff xml
SET #xmlstuff = '3'
SELECT #xmlstuff.value('.', 'money') as dollars
SQL Fiddle
The 2nd parameter of value() is the data type of return value. And the first parameter is XQuery expression to locate the value within XML structure. Assuming you don't have any structure in the XML -simply the value, because of which you expect it to be convertible directly using CAST-, you can always pass . as the 1st parameter.

Resources