This question already has answers here:
SQL: How can I get the value of an attribute in XML datatype?
(4 answers)
Closed 4 years ago.
how can i find and split a value between double quotation
Value is like this :
'<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
value is one filed of table and i want split "EC,ETC,Val" value
i mean 180,711,1679
thanks for your help
You Can try the following
DECLARE #XMLData XML = '<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
SELECT
EC = Node.Data.value('#EC', 'INT'),
ETC = Node.Data.value('#ETC', 'INT'),
Val = Node.Data.value('#EC', 'INT')
FROM #XMLData.nodes('/Relations/Form') Node(Data)
Related
This question already has answers here:
getting "invalid column" when trying to use column alias in a query
(3 answers)
Closed 1 year ago.
SELECT .....
'NewReserveSum' =
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end,
(o.ReserveSummN - NewReserveSum) as 'DifferenceReserveSummN'
FROM ......
I want to use the ** NewReserveSum ** variable in the following columns, but I got an error:
Invalid column name 'NewReserveSum'
How can I do it correctly in MSSQL?
In SQL, = is comparison, not assignment
In a SELECT statement, you can use AS to give a name top an expression column:
SELECT
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end as NewReserveSum,
...
This question already has answers here:
SQL How to Select the most recent date item
(6 answers)
Closed 2 years ago.
I would like to select last inserted data in my table.
I tried :
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=MAX(DateSpread)"
But this doesn't work -> returns : Error Automation..
MAX(DateSpread) should return 04/11/2020
DateSpread is a date format in my AccessDB
And when I hard code:
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=04/11/2020"
It does work, what am I missing?
Please note that I execute this SQL request in Excel and my database is an Access database (.accdb)
Then answer was that I had to do an other select :
"SELECT * FROM HistoSpreadLiq WHERE DateSpread=(SELECT Max(DateSpread) FROM HistoSpreadLiq)"
This question already has answers here:
SQL Server Xml query with multiple namespaces
(3 answers)
Closed 3 years ago.
The problem
I need to take data from XML files provided by another application. These XMLs contain data that is required to be inserted into tables for a database for a different application.
The problem is this, I can't get the values out of the XML file. I keep getting blank/null values instead.
I am using SQL Server 2014 Management Studio.
What I've tried
I've looked into how to read an XML and have that placed into a table. I've gone as far as looking at tutorials that work. But, whenever I try to apply the same process to my tables and using my XML data I keep getting blank values.
Some help would be appreciated as I have been looking into this for a day and researching how to approach this using MS SQL. And I cannot for the life of me understand why I keep getting nulls.
The XML
<DistributionOrder xmlns="a-url">
<Date>2019-03-07T14:38:00</Date>
<Order_Number>ACME01</Order_Number>
<Comment/>
<Status>Active</Status>
<Lines>
<Line>
<Order_Line_Number>1</Order_Line_Number>
<Product>22</Product>
<Status_Line>Active</Status_Line>
<Comment_Line/>
<Location>ENT_78</Location>
</Line>
</Lines>
</DistributionOrder>
The query
DECLARE #xmlData XML;
INSERT INTO dbo.XMLwithOpenXML(XML_data)
SELECT *
FROM OPENROWSET(BULK 'C:\test_file.xml', SINGLE_BLOB) AS ImportSource;
SELECT * FROM XMLwithOpenXML;
SELECT
element.value('(Order_Line_Number/text())[1]', 'nvarchar(80)') AS Order_Line_Number,
element.value('(Product/text())[1]', 'nvarchar(80)') AS Product
FROM
XMLwithOpenXML
CROSS APPLY
XML_data.nodes('DistributionOrder/Lines/Line') AS DO(element)
GO
What I expect
Order_Line_Number | Product
------------------+----------
1 | 22
What I Get
Order_Line_Number | Product
------------------+----------
|
Figured out that the,
<DistributionOrder xmlns="a-url">
</DistributionOrder>
was the problem. I deleted the xmlns="a-url" from the xml and found that my code is working.
Discovered that you can use
;WITH XMLNAMESPACES(DEFAULT 'a-url')
To take the namespace into account.
This question already has an answer here:
Query Data from XML
(1 answer)
Closed 5 years ago.
I have a table in an Oracle 12c Database that contains a XML Type Column and i need to extract a value from this Column.
Here an example of this Column's content:
<AbcMsg xmlns="http://example.org/SCL/CommonTypes">
<Msg>
<Pmnt>
<swif:SWIFT MT="202" xmlns:swif="urn:abc/scl/SWIFT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<swif:Header>
<swif:Input_Output_Identifier>O</swif:Input_Output_Identifier>
<swif:Sender_BIC>XXXXXXXXXXX</swif:Sender_BIC>
<swif:Receiver_BIC>XXXXXXXXXXX</swif:Receiver_BIC>
<swif:Message_Priority>NORM</swif:Message_Priority>
<swif:User_Header>
<swif:Message_User_Reference>XXXXXXXXXXXXXXXXX</swif:Message_User_Reference>
</swif:User_Header>
<swif:Original_Swift>
<swif:Message>DATA TO BE EXTRACTED</swif:Message>
</swif:Original_Swift>
<swif:Create_Date_Time>2016-07-28T15:45:00</swif:Create_Date_Time>
<swif:Msg_ID>POFu7yCXHoN</swif:Msg_ID>
<swif:Sequence_Number>248600</swif:Sequence_Number>
<swif:Session_Number>6184</swif:Session_Number>
</swif:Header>
<swif:Data xsi:type="swif:Data_202">
<swif:F20>
<swif:Sender_s_Reference>POFu7yCXHoN</swif:Sender_s_Reference>
</swif:F20>
<swif:F21>
<swif:Related_Reference>XXXXXXXXXXX</swif:Related_Reference>
</swif:F21>
<swif:F32A>
<swif:Currency>USD</swif:Currency>
<swif:Amount>156020000</swif:Amount>
<swif:Date>2016-07-28</swif:Date>
</swif:F32A>
<swif:Cdrt_F58a_F59a>
<swif:Account>174208531</swif:Account>
<swif:Identifier_Code>XXXXXXXXX</swif:Identifier_Code>
</swif:Cdrt_F58a_F59a>
</swif:Data>
</swif:SWIFT>
</Pmnt>
<Extn/>
</Msg>
</AbcMsg>
I need to retrieve the Value of <swif:Original_Swift>. I've tried the function EXTRACT(xml_column, '/AbcMsg/Msg/Pmnt') but it keeps returning null.
Any clue on how to do that???
Thanks in Advance.
Try xmltable or xmlquery. You have to declare defult and swif namesapce;
select * from xmltable(XMLNAMESPACES('urn:abc/scl/SWIFT' as "swif", default 'http://example.org/SCL/CommonTypes') , '/AbcMsg/Msg/Pmnt/swif:SWIFT/swif:Header/swif:Original_Swift' passing xmltype(your_xml_here));
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
mysql_insert_id alternative for postgresql
Is there a Postgresql equivalent of mysql_insert_id() to get the ID generated in the last query ??
If you are using PostgreSQL 8.2 or newer then the simplest way is to use RETURNING:
$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_row($result);
$insert_id = $insert_row[0];
For other alternatives see this duplicate.