Passing column/variable as binary value to convert function - sql-server

I am manipulating a large value containing rows and columns values separated with ASCII row and column separators. The result is tabular data which I am inserting in SQL table.
Simple example to get the idea:
George|20;Ivan|15;Peter|10;
is transform to:
George 20
Ivan 15
Peter 10
and inserted in Users ([name], [age]) table.
Before insert, each value is converted to its column type using TRY_CONVERT function. The issue is this does not work as expected with VARBINARY data. For example (the second output is correct):
DECLARE #A NVARCHAR(1024) = '0xFE520676B1A1D93DABAB2319EEA03674F3632EAEEB163D1E88244F5EB1DE10EB';
SELECT TRY_CONVERT(VARBINARY(255), #A)
-- 0x300078004600450035003200300036003700360042003100410031004400390033004400410042004100420032003300310039004500450041003000330036003700340046003300360033003200450041004500450042003100360033004400310045003800380032003400340046003500450042003100440045003100300045004200
SELECT TRY_CONVERT(VARBINARY(255), 0xFE520676B1A1D93DABAB2319EEA03674F3632EAEEB163D1E88244F5EB1DE10EB)
-- 0xFE520676B1A1D93DABAB2319EEA03674F3632EAEEB163D1E88244F5EB1DE10EB
When the VARBINARY data is passed as string, the string is converting itself to VARBINARY. I need to pass the data without quotes, but how to do this when a column/variable of type string is passed?

I needed to use the style option of the TRY_CONVERT function.
DECLARE #A NVARCHAR(1024) = '0xFE520676B1A1D93DABAB2319EEA03674F3632EAEEB163D1E88244F5EB1DE10EB';
-- using style option 1
SELECT TRY_CONVERT(VARBINARY(255), #A, 1)
SELECT TRY_CONVERT(VARBINARY(255), 0xFE520676B1A1D93DABAB2319EEA03674F3632EAEEB163D1E88244F5EB1DE10EB)

Related

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?

Split TEXT data type variable parameter in SQL stored procedure

l had a parameter in my stored procedure that was varchar(2000), my problem now is the characters being sent on the parameter have grown, the parameter now needs to handle 30 000 characters which are comma-separated. The biggest variable type that am now using (TEXT) is giving me issues in my where clause as below
Now using TEXT data type
CREATE PROCEDURE
(#ArrayList TEXT ='' -- '93238128,93238131,93238130,93238133,93238132 ......
)
then in my where clause
WHERE c.CLAIM_ID IN (SELECT Element FROM dbo.Split(#ArrayList ,','))
AND #ArrayList <>'') OR #ArrayList =''
Getting this error :
The data types text and varchar are incompatible in the not equal to operator.
Here's what l ended up using Tables! And it works great. Sending a table with one column to a stored procedure.

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

Get only numeric part of a column data?

I have a column in my database containing both numeric and alphanumeric characters.
I only want to get the numeric (6 numbers) from the column.
Example of data:
TEST_123456_Prod
DB111111P
F222222FN
PROD999999_SCF
I want to create a select statement that returns all rows from this column where all but numbers are filtered out.
I´m using SQL Server, so probably Charindex needs to be used, but no idea how.
To strip off all alphanumeric characters you can create a function as:
CREATE FUNCTION [dbo].[RemoveAlphaCharacters](#InputString VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',#InputString)>0
SET #InputString = STUFF(#InputString,PATINDEX('%[^0-9]%',#InputString),1,'')
RETURN #InputString
END
GO
and then use it to get desired result as:
select dbo.RemoveAlphaCharacters(databasename)
from T1;
SQL Fiddle
This will work for all of your examples:
SELECT
SUBSTRING(databasename,
PATINDEX('%[0-9]%', databasename),
LEN(databasename) - (PATINDEX('%[0-9]%', REVERSE(databasename)) + PATINDEX('%[0-9]%', databasename)) + 2)
FROM dbs
Here is a SQLFiddle with how the code works.

SQL Server: How do I use modify() to alter XML data in a column with a TEXT data type

I'm trying to modify some XML values in a database. I can get it to work on columns that contain XML that are using the XML data type. However, I can't get it to work on TEXT columns.
Also, I can SELECT XML data on TEXT columns (by using CAST() to convert it to XML), but still can't UPDATE.
Example:
UPDATE [xmltest]
SET [xmltext].modify('replace value of (/data/item[1]/text())[1] with "newvalue"')
Error: Cannot call methods on text.
Is there some way I can get this to work on a TEXT column? There's already TONS of data stored, so I'd rather not have to request to change the data type on the column.
Thanks!
Sunsu
You cannot directly modify this - what you can do is a three steps process:
select the TEXT column from the table into a local XML variable
modify the XML variable
write back your changes to the database
Something like this:
-- declare new local variable, load TEXT into that local var
DECLARE #temp XML
SELECT
#temp = CAST(YourColumn AS XML)
FROM
dbo.YourTable
WHERE
ID = 5 -- or whatever criteria you have
-- make your modification on that local XML var
SET
#temp.modify('replace value of (/data/item[1]/text())[1] with "newvalue"')
-- write it back into the table as TEXT column
UPDATE
dbo.YourTable
SET
YourColumn = CAST(CAST(#temp AS VARCHAR(MAX)) AS TEXT)
WHERE
ID = 5 -- or whatever criteria you have
It's a bit involved, but it works! :-)

Resources