I am facing an issue while converting a query from Oracle to snowflake. Could you please help out.
Sample Oracle query:
replace(REGEXP_SUBSTR( col_name,'(.*?)([[:space:]]>>[[:space:]]|$)', 1,1 ) , ' >> ','') as test
It seems Snowflake behaves different when processing (.*?) part of your regular expression. As a workaround, you may use [^>]* or \w+ instead of (.*?):
SELECT
replace(REGEXP_SUBSTR( 'test1 >> test2 >> test3','([^>]*)([[:space:]]>>[[:space:]]|$)', 1,1 ) , ' >> ','') as test;
SELECT
replace(REGEXP_SUBSTR( 'test1 >> test2 >> test3','\\w+([[:space:]]>>[[:space:]]|$)', 1,1) , ' >> ','') as test;
These should give the same result ("test1") with Oracle's REGEXP_SUBSTR.
Related
update a
set a.col1 = 1,
a.col2 = 'abc' + CAST(b.colID AS VARCHAR(3))
from #abc a
cross apply
(select top 1 x.col3, x.colID1
from #xyz x
where (x.col3 = a.col3 and
x.colID1 IN (0, 9, 8))) b
Table abc and xyz are temp tables that's why the '#'.
Need to convert the above script to Oracle
below link could help you what you want to achive...
https://oracle-base.com/articles/18c/private-temporary-tables-18c
I'm playing how I can insert some spec char in my stored procedure in SQL so my SSRS box will put them in new lines (* please don't confuse with any SSRS formatting/expression, char(10), vbcrlf etc...). Additionally my input comes from xml like in snippet below, I tried all and nothing worked, I also tried to insert $ and later replace in SSRS box and this also strangely didn't work, is it possible to do ideally without going into SSRS formatting ?
Thanks all !
*** =Replace("$",vbcrlf)
; WITH cte AS (SELECT 'Line aaaaaa' Order_Desc UNION Select 'Line 22222' Proc_CODE UNION SELECT 'Line 33333333' Proc_Code )
select
STUFF((SELECT
', ' + ord.Order_Desc As [text()]
--',$' + ord.Order_Desc As [text()]
FROM cte ord
ORDER BY ord.Order_Desc
FOR XML PATH('')
),1,2,'') AS Order_All
DECLARE #LineBreak NVARCHAR(100)
SET #LineBreak = 'First line content.' + CHAR(13)+CHAR(10) + 'Second line
content.'
PRINT #LineBreak
While I've been troubled by annoying SQL operations, I've got another T-SQL problem.
I have to convert some Int variables into zero-filled varchar with plus/minus sign.
I have tried RIGHT() and CONVERT() functions so far,
RIGHT('000000' + CONVERT(value1 AS VARCHAR(6)), 6)
but I couldn't figure out what the best way to convert them.
What I would like to see is:
If value1 = 305 then '+000305'
If value1 = -110 then '-000110'
Is there any simple function to achieve this? Or should I have to create my own function for this?
I know it's stupid question, but my current task requires a lot of such non-sense tasks to Stored Procedures (database-side) rather than 'application-side'.
Thanks.
You want to use CAST instead of CONVERT when you structure it that way. There's no standard function that will do what you want, but you could create an expression like this to get what you need:
LEFT(REPLACE(SIGN(value1),'1','+'),1) + RIGHT('000000' + CAST(ABS(value1) AS VARCHAR(6)), 6)
If you want to use CONVERT it would look like:
LEFT(REPLACE(SIGN(value1),'1','+'),1) + RIGHT('000000' + CONVERT(VARCHAR(6),ABS(value1)), 6)
Just for fun, here's another way you could do it:
LEFT(REPLACE(SIGN(value1),'1','+'),1) + REPLACE(STR(ABS(value1),5),' ','0')
For SQL 2012 and above, you can use FORMAT().
DECLARE #yourTable TABLE (nums INT);
INSERT INTO #yourTable VALUES (305),(-110);
SELECT RIGHT('+' + FORMAT(nums,'00000#'),7) AS formatted_nums
FROM #yourTable
Results:
formatted_nums
--------------
+000305
-000110
may be this one can achieved in both ways using REPLACE or Replicate function.
DECLARE #t TABLE (Num INT)
INSERT #t
SELECT 305
UNION SELECT -110
SELECT CASE WHEN Num < 0 THEN '-' ELSE '+' END +
replace(RIGHT('00000'+ CONVERT(VARCHAR,Num),8),'-','0') AS NUM
FROM #t
SELECT CASE WHEN Num < 0 THEN '-' ELSE '+' END +
RIGHT(Replicate('0',6) + CONVERT(VARCHAR,Abs(Num)), 6)
FROM #t
I'm sure somebody will know of a app or some website to help do this:
I need to run a script in the 'database engine tuning advisor', but would like to do all or most of the possible combinations of variables for my select statement/function.
For example, I have:
#RegionID, can be any value from SELECT EntityGroup.Id FROM EntityGroup (e.g. 1,2,3,4)
#LanguageId, can be any value from SELECT Language.Id (e.g. en-GB, tr-TR)
#Group1, can be 1,2,3,4,5,6,7
and so on.
then to have something generate an sql script such as
SELECT * From xyz (1, 'en-GB', 1)
SELECT * From xyz (1, 'tr-TR', 1)
SELECT * From xyz (2, 'en-GB', 1)
SELECT * From xyz (2, 'tr-TR', 1)
over and over with each of the possible combinations of variables.
Any tips?
thanks
You can run this query:
SELECT 'SELECT * FROM xyz (' + CAST(A.Id AS VARCHAR(10)) + ', ''' +
B.Id + ''', ' + CAST(C.Id AS VARCHAR(10)) + ')' AS Script
FROM EntityGroup A
CROSS JOIN [Language] B
CROSS JOIN [Group] C
And then copy the results to get your script. (Though you need to know that if there are more than just those values on the tables, the CROSS JOIN will rapidly grow in size).
From SQL Server 2005, I need to do an export for a client to an xml-like format shown below. Why they are not using XML, I don't know. Any ideas on how to do this quickly without exporting the file "FOR XML" and then writing a messy script to go through the text file and find and replace <, >, and every closing XML tag? Thank you.
START_FILE:
DATE:
COLUMN1:A
COLUMN2:B
COLUMN3:C
COLUMN1:D
COLUMN2:E
COLUMN3:F
COLUMN1:G
COLUMN2:H
COLUMN3:I
END_FILE:
DECLARE #Output nvarchar(max)
SET #Output = 'START_FILE:
DATE:'
SELECT #Output = #Output + '
COLUMN1:' + Col1 + '
COLUMN2:' + Col2 + '
COLUMN3:' + Col3
FROM YourTable
ORDER BY Col1
SELECT #Output + '
END_FILE:' AS Result
I went with Martin's suggestion of trying an UNPIVOT query, which was all new to me. Using SSIS, I am now exporting the query to a text file formatted exactly as I need it with a run time of just a few seconds. I am using a query like below with a ":" as a column delimiter. Great suggestion Martin!
SELECT 'START_FILE' as FieldName, '' as 'FieldValue'
UNION ALL
select 'DATE' as FieldName, getDate() as 'FieldValue'
UNION ALL
SELECT FieldName, FieldValue
FROM
(
SELECT
Cast(Column1Name as varchar) as VendorColumn1Name,
Cast(Column2Name as varchar) as VendorColumn2Name,
Cast(Column3Name as varchar) as VendorColumn3Name
FROM MyTable
) c
UNPIVOT
(
FieldValue for FieldName IN(VendorColumn1Name, VendorColumn2Name, VendorColumn3Name)
) as p
UNION ALL
SELECT 'END_FILE' as FieldName, '' as 'FieldValue'