How to use extractvalue in SQL Server - sql-server

I am trying to convert my Oracle query to SQL Server. The query shown below is causing some difficulty when trying to convert EXTRACTVALUE, XMLSEQUENCE, XMLTYPE functions.
Can anyone help me learn how to convert these to SQL Server?
EXTRACTVALUE(COLUMN_VALUE, 'pipeline/#name') NAME
FROM TABLE (SELECT XMLSEQUENCE(XMLTYPE(MESSAGE).EXTRACT('processEngine'))
FROM NMS_MESSAGES WHERE OBJECT_CODE='pe_monitor' AND ID=#WHERE:PARAM:USER_DEF:INTEGER:PARAM_PROCESS_ENGINE#)) INSTANCENAME
,(SELECT EXTRACTVALUE(COLUMN_VALUE, 'processEngine/#id') FROM TABLE (SELECT XMLSEQUENCE(XMLTYPE(MESSAGE).EXTRACT('processEngine'))
FROM NMS_MESSAGES WHERE OBJECT_CODE='pe_monitor' )) ENGINE_ID
Thanks in advance.

Think about XQuery. As I See that EXTRACTVALUE seems like #xml.values('(#node/#attr)[1]','type') and EXTRACT is #xml.nodes('path/path/path')
If know structure of xml that you ought to parse exactly than you will convert this query without any problems.

Related

Is there a way to extract individual values from a varchar column using SQL Server 2016?

I am trying to extract individual dates from a varchar column in a SQL Server 2016 tablet that are stored comma separated and am not sure how to proceed. The data is setup like this:
article Consolidation_Order_Cut_Off_Final_Allocation
------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
011040 01/13/2021,03/10/2021
019099 01/13/2021,01/27/2021,02/24/2021,03/24/2021,04/28/2021,05/26/2021,06/23/2021,07/28/2021
019310 01/27/2021,02/03/2021,03/10/2021,04/14/2021,05/12/2021,06/09/2021,07/14/2021,08/11/2021
059611 01/13/2021
Ideally - I would have each date split out into a new row. I have seen a few similar questions that use very complex functions but those seem to be for SQL Server 2008. I have also found the new function STRING_SPLIT but that would seem to be table valued and thus have to come in the FROM. One thought I had was to declare a variable to hold this column and then use something like select * FROM string_split(#dates,','); however since there is more than one value in that list that returns an error. I am very new to the 2016 version of SQL Server and curious if anyone has ran into a way to solve this.
String_Split() is a table valued function, so you can call it with a CROSS APPLY
Example or dbFiddle
Select A.article
,B.Value
From YourTable A
Cross Apply string_split(Consolidation_Order_Cut_Off_Final_Allocation,',') B

Need Help Converting Oracle Query to SQL Server

Several weeks ago I made a post to get help with converting a comma delimited list of values into a format that it could be used as part of an IN clause in Oracle. Here is a link to the post.
Oracle invalid number in clause
The answer was to split up the list into an individual row for each value. Here's the answer that I ended up using.
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
FROM ( SELECT '1,2,3,4' str FROM dual )
CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
Is there a way that I can do something similar in SQL Server without having to create a custom function? I noticed that there's a STRING_SPLIT function, but I don't seem to have access to that on this SQL Server.
Any advice you might have would be greatly appreciated. I've been trying to take a stab at this for the majority of the day.
String_split function is available in MS SQL Server starting from version 2016. If you use older version you can write a few lines of code which do the same.
declare #str varchar(100)='1,2,3,4' --initial string
;with cte as (--build xml from the string
select cast('<s>'+replace(#str,',','</s><s>')+'</s>' as xml) x
)
--receive rows
select t.v.value('.[1]','int') value
from cte cross apply cte.x.nodes('s') t(v)

Convert Access query to SQL

I am converting Access query to SQL Server.
I want to convert below lines to SQL
1. Format (210.6, "Standard")
2. Format (210.6, "#,##0.00")
How do i convert it to SQL query.
I have tried with below, but still not able to find the solution.
For the first query, i found below solution, which is correct
1. CONVERT(varchar, CAST(tSRO.OutputF11 AS money), 1)
Now, for second query, i do not know what i have to do.
From SQL Server 2012+ you can use FORMAT:
SELECT FORMAT(210.6, '#,##0.00') -- 210.60
SELECT FORMAT(1210.6, '#,##0.00') -- 1,210.60
LiveDemo
SQL Server before 2012:
SELECT REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1210.6),1),'.00','') -- 1,210.60
LiveDemo2
Warning:
This operation is pure for presentation layer and should be done in application.

Mysterious:Selecting a large Xml in Sql Server

I am having a column in my table which stores XML data as a varchar(MAX).For example one of my value has around 1 LAC characters.While selecting it from the Table, i end up getting only 43679 characters for all samples.
What is the reason behind this mystery?Please,if there is any way to retrieve the complete data,help.
Try using settings of sql server management studio.
try to select with a cast:
select top 1 cast(ColumnName as xml) from Table

How to show decimal zeroes for a number in SQL Server

I am pulling the following numbers from MS SQL Server:
345
345.6
345.67
How do I pull them so that they look like the following:
345.00
345.60
345.67
What datatype should they be stored as and what is the magic function to pull them?
Thank you.
You can use a cast function. Since you are pulling out values of a table, you would be using a select and some thing like the following can be used-
Select CAST(value AS DECIMAL(5,2)) from table
I hope that is what you are looking for.
cheers

Resources