Below is the teradata code i want to convert to snowflake
select XMLAGG (XMLELEMENT (n, col1
|| CHR (10))
ORDER BY num
).EXTRACT ('//text()').getClobVal () AS abc
tried as below but not working. am new to both oracle and snowflake please help me with this
extract(listagg (parse_xml('<n> col1|| CHR (10) <n>')),'//text()')
Almost certainly your Teradata code is simply a polyfill (aka a work around) for the historic lack of LISTAGG in Teradata.
Likely you simply need this code
SELECT
LISTAGG(col1,'\n') WITHIN GROUP (ORDER BY num)
Related
I'm trying to convert 4 date columns titled Created, Approved, Processed & Realized to a single column with all 4 dates AND a second column with the status of each of those dates.
The image at the end shows the data issue visually (apologies I'm still figuring out how to attach tables in textual form on stackoverflow)
To solve this, I successfully executed the CROSS APPLY
function in SQL server (see below), but I now need to do the same in AWS Simba
Athena or the Presto language. Can someone please guide me on what is the AWS/Presto equivalent of a CROSS APPLY function? Thank you in advance
SELECT
V.Date,
V.Status
From Table C
CROSS APPLY
(VALUES
(C.Created, 'Opened'),
(C.Approved, 'Approved'),
(C.Processed, 'Processed'),
(C.Realized, 'Realized')
) AS V([Date], Status)
I want to convert the following table:
You should be able to use UNNEST for this:
SELECT v.date, v.status
FROM m_table
CROSS JOIN UNNEST(ARRAY[
ROW(C.Created, 'Opened'),
ROW(C.Approved, 'Approved'),
ROW(C.Processed, 'Processed'),
ROW(C.Realized, 'Realized')
]) AS v(date, status);
This works in the latest Presto version, 337.
In Athena you probably still cannot UNNEST array or ROW
in ANSI SQL manner, so you may need some modifications.
Below is an Oracle script that I need to execute on an SQL Server.
SELECT
records.pr_id,
SUBSTR (REPLACE (REPLACE (XMLAGG (XMLELEMENT ("x", prad4.selection_value)
ORDER BY prad4.selection_value),'</x>'),'<x>',' ; '),4) as teva_role
FROM records
Thanks for the help,
Barry
I programmed in SQL for years in several environments and it is about 75% the same. So, the SQL statement should work as is, however the functions (REPLACE, SUBSTR) will be what you need to research and change.
Also, you get columns from prad4 without including it in the FROM statement which is a problem.
And, finally, your parentheses aren't balanced which, I would think, would be a problem in Oracle as well.
This is basically concatenating a set of strings with a delimiter. The common way to do this, is using FOR XML PATH('') which seems to be the equivalent of the combination of XMLELEMENT() in Oracle, but with a different syntax. You can also use XML functions to prevent change of certain characters not allowed in XML. The STUFF takes care of the SUBSTR() part of your code. For a more detailed explanation, you can read this article on Creating a comma-separated list.
The code should look similar to this:
SELECT records.pr_id,
STUFF(( SELECT ' ; ' + prad4.selection_value
FROM prad4
WHERE prad4.pr_id = records.pr_id
ORDER BY prad4.selection_value
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 3, '')
FROM records;
Of course, with the improvements of SQL Server 2017, the code can be simplified to something like this:
SELECT records.pr_id,
STRING_AGG( selection_value, ' ; ') WITHIN GROUP (ORDER BY selection_value ASC)
FROM records;
server experts,
can anyone say if MSSQL server (v. 16) can save a returned sql-query in json-format into another db-table (into just one db-field as json string)? After that the json- as a string is to be queried from this table-field.
If that is possible, how to implement it?
Yes, but not directly, you need a little bypass. Consider the following:
WITH CTE (JSON_Col) AS -- end previous statement with semi-colon
(
SELECT ColA, ColB, ColC
FROM TableA FOR JSON AUTO
)
SELECT JSON_Col INTO ResultsTable FROM CTE
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)
I am trying to write a simple query in order to change some of our stage data. I have a varchar $ column (unfortunately) that needs to be summed. My issue is that because of commas, I cannot change the datatype.
So, I can use REPLACE(AMT,',','') to remove the commas but It still wont let me cast it as a decimal and I get
Error converting data type varchar to numeric.
I am trying the following below with no luck. Any ideas? Can this be done or am I using the wrong syntax here?
Select SUM(Cast(REPLACE(Amt,',','') as Decimal (18,2)) )
I was able to resolve this with #HABO suggestion. I used Cast(Ltrim(rtrim(table.AMT)) as Money) for all instances of the varchar amount. This removed white space and removed the commas from the numbers.
This should work... including an example.
Edit: if you are on SQL Server 2012+, you may be able to shorten your task by using Try_Convert
DECLARE #SomeTable AS TABLE (Amt Varchar(100));
INSERT INTO #Sometable (Amt) VALUES ('abc123,456.01'),(' 123,456.78 '),(Null),('asdad'),('');
With NumericsOnly AS
(
SELECT
REPLACE(Left(SubString(Amt, PatIndex('%[0-9.-,]%', Amt), 8000), PatIndex('%[^0-9.,-]%', SubString(Amt, PatIndex('%[0-9.,-]%', Amt), 8000) + 'X')-1),',','') AS CleanAmt
FROM
#SomeTable
)
SELECT
SUM(CONVERT(DECIMAL(18,2), CleanAmt)) AS TotalAmt
FROM
NumericsOnly
WHERE
IsNumeric(CleanAmt)=1
General methodology is taken from here
I wouldn't use money as a data type as it is notorious for rounding error.
The error is due to SQL order of operations within your SUM(CAST(REPLACE... operation. This issue can be resolved by summing the column AFTER it's been staged to be summed via a subquery:
SELECT SUM(Field),...
FROM ( SELECT
Cast(REPLACE(Amt,',','') as NUMERIC) as 'Field'
,...
) [Q]
If the table you're summing is administered by a BI Team, get them to stage the data there. Happy Data Happy life.