Extract substring from file path in snowflake - snowflake-cloud-data-platform

I need to extract substring from list of file paths. Paths are like "\rootfolder\subfolder". Need extract substring as "rootfolder" ( between 2nd backslash and 3rd backslash.
select SUBSTR('\\rootfolder\subfolder\', 3, (REGEXP_INSTR('\\rootfolder\subfolder\','\', 1, 3) - 3)) from table;
But this expression didn't give right result in Snowflake. problem is with backslash character.

Using SPLIT_PART:
SELECT SPLIT_PART($$\\rootfolder\subfolder\$$, $$\$$, 3);
--
SELECT SPLIT_PART('\\\\rootfolder\\subfolder\\', '\\', 3);
Output:
Dollar quoting was used to avoid doubling \.

Related

Replace + (Plus) Character using a New Line

I have column which has a long string with multiple sentences. Each sentence is separated by a plus (+) character. For an example: 1. This is line one + 2. This is line two + 3. This is line Four
Now I would like to replace the plus (+) character by a new line. Using T-SQL how can I get the output like:
This is line one
This is line two
This is line three
Just use REPLACE and replace the + character with a carriage return and line break:
SELECT REPLACE(YourString,'+', CHAR(13) + CHAR(10)) AS YourString
FROM dbo.YourTable;

SQL Select statement until a character

I'm looking to extract all the text up until a '\' (backslash).
The substring is required to remove all proceeding characters (17 in total) and so I would like to return all after the 17th until it comes across a backslash.
I've tried using charindex but it doesn't seem to stop at the \ it returns characters afterward. My code is as follows
SELECT path, substring(path,17, CHARINDEX('\',Path)+ LEN(Path)) As Data
FROM [Table].[dbo].[Projects]
WHERE Path like '\ENQ%\' AND
Deleted = '0'
Example
The below screen shot shows the basic query and result i.e the whole string
I then use substring to remove the first X characters as there will always be the same amount of proceeding characters
But what Im actually after is (based on the above result) the "Testing 1" "Testing 2" and "Testing ABC" section
The substring is required to remove all proceeding characters (17 in total) and so I would like to return all after the 17th until it comes across a backslash.
select
substring(path,17,CHARINDEX('\',Path)-17)
from
table
To overcome Invalid length parameter passed to the LEFT or SUBSTRING function error, you can use CASE
select
substring(path,17,
CASE when CHARINDEX('\',Path,17)>0
Then CHARINDEX('\',Path)-17)
else VA end
)
from
table

T-SQL 2008: Parse String

I have the following location as a string:
\\Windows\UnitB\CU1234_001\
I want to return the CU1234_001 part only. The query which I need to use needs to be dynamic since this string will change and it could be longer or shorter (it will all the time end in "\".
I've tried to used something like this but this just eliminate the last "\" and returns the rest of the string:
select
substring('\\Windows\UnitB\CU1234_001\',
1, (len('\\Windows\UnitB\CU1234_001\') - (Charindex('\',
reverse(rtrim('\\Windows\UnitB\CU1234_001\'))))))
You can use a combination of string functions to extract what you want:
SELECT REVERSE(SUBSTRING(REVERSE(col),
2,
CHARINDEX('/', REVERSE(col), 2) - 2))
FROM yourTable

SQL: Fix for CSV import mistake

I have a database that has multiple columns populated with various numeric fields. While trying to populate from a CSV, I must have mucked up assigning delimited fields. The end result is a column containing It's Correct information, but also contains the next column over's data- seperated by a comma.
So instead of Column UPC1 containing "958634", it contains "958634,95877456". The "95877456" is supposed to be in the UPC2 column, instead UPC2 is NULL.
Is there a way for me to split on the comma and send the data to UPC2 while keeping UPC1 data before the comma in tact?
Thanks.
You can do this with string functions. To query the values and verify the logic, try this:
SELECT
LEFT(UPC1, CHARINDEX(',', UPC1) - 1),
SUBSTRING(UPC1, CHARINDEX(',', UPC1) + 1, 1000)
FROM myTable;
If the result is what you want, turn it into an update:
UPDATE myTable SET
UPC1 = LEFT(UPC1, CHARINDEX(',', UPC1) - 1),
UPC2 = SUBSTRING(UPC1, CHARINDEX(',', UPC1) + 1, 1000);
The expression for UPC1 takes the left side of UPC1 up to one character before the comma.
The expression for UPC2 takes the remainder of the UPC1 string starting one character after the comma.
The third argument to SUBSTRING needs some explaining. It's the number of characters you want to include after the starting position of the string (which in this case is one character after the comma's location). If you specify a value that's longer than the string SUBSTRING will just return to the end of the string. Using 1000 here is a lot easier than calculating the exact number of characters you need to get to the end.

SQL Server : select statement where value have format

I'm trying to find special string that has a format like ##-%, start from two character and '-'
select *
from temp
where name like '##-%'
Give me some advice, or example how I can get this?
And how can I replace this format on empty string ""
If would you find the special character which is start from two character and '-'
select * from temp where name like '[a-z][a-z]-%'
if would you want to find the special string have format like '##-%
select * from temp where name like '##-[%]%'
If would you want to find the third letter is '-'
select * from temp where name like '__-%'
Add underscores for how many characters you need at start.In your case its two __
select * from temp where name like '__-%'
It's quite simple. Just use [ ] around '%' to escape it.
Select * From temp
Where name like '##-[%]%'
This will return any record where the 3rd character in the 'name' column is '-'
SELECT *
FROM temp
WHERE SUBSTRING(name, 3, 1) = '-'

Resources