Snowflake Syntax - Unexpected space character in Syntax - sql

Query
select sis.subject_code||'_'||LEFT(REPLACE(sis.SIS_TERM_ID,0,''),LENGTH(sis.SIS_TERM_ID) - 4)||''|| REPLACE(SUBSTR(sis.SIS_TERM_ID, 8, 8),'','')
from TableX;
Result looks like the following
XXXX888543_134 1 ---there is a space before the last value. I am not sure where this is being derived from. Any ideas on what I could modify in the string above please.

Assuming the space is really a space, how about doing the replace() across the whole string?
select replace(sis.subject_code || '_' || LEFT(REPLACE(sis.SIS_TERM_ID, 0, ''), LENGTH(sis.SIS_TERM_ID) - 4) || SUBSTR(sis.SIS_TERM_ID, 8, 8), '', '')
It is unclear whether the replace is coming from the last element or the one before it. But you don't seem to want any spaces in the string.

Related

how to replace dots from 2nd occurrence

I have column with software versions. I was trying to remove dot from 2nd occurrence in column, like
select REGEXP_REPLACE('12.5.7.8', '.','');
expected out is 12.578
sample data is here
Is it possible to remove dot from 2nd occurrence
One option is to break this into two pieces:
Get the first number.
Get the rest of the numbers as an array.
Then convert the array to a string with no separator and combine with the first:
select (split_part('12.5.7.8', '.', 1) || '.' ||
array_to_string((REGEXP_SPLIT_TO_ARRAY('12.5.7.8', '[.]'))[2:], '')
)
Another option is to replace the first '.' with something else, then get rid of the '.'s and replace the something else with a '|':
select translate(regexp_replace(version, '^([^.]+)[.](.*)$', '\1|\2'), '|.', '.')
from software_version;
Here is a db<>fiddle with the three versions, including the version a_horse_with_no_name mentions in the comment.
I'd just take the left and right:
concat(
left(str, position('.' in str)),
replace(right(str, -position('.' in str)), '.', '')
)
For a str of 12.34.56.78, the left gives 12. and the right using a negative position gives 34.56.78 upon which the replace then removes the dots

Isolating an alphanumerical ID that can be anywhere in a text based cell

I'm trying to isolate a certain character string from a text cell.
For example, I would like to extract "AB-T120-15" from the string "His server ID was AB-T120-15 and his problem was that he needed a reboot"
AB-T120-15 is an example, but they would all be codes of a max length of 13 characters starting by something like AB-T, CL-R, etc.
The codes can appear anywhere in a text field of the column.
string_split() cannot be used since the DB we are under is older.
I have tried many combinations of Substring and LEFT, but I cannot seem to have it worked.
Any thoughts?
String operations are not the strength of SQL Server -- which I assume you are using.
You can do this with rather painful string manipulation:
select left(stuff(str, 1, patindex('%[A-Z][A-Z]-[A-Z]%', str) - 1, ''),
charindex(' ', stuff(str, 1, patindex('%[A-Z][A-Z]-[A-Z]%', str), '') + ' ')
)
from (values ('His server ID was AB-T120-15 and his problem was that he needed a reboot')) v(str);

SQL I need to extract a stored procedure name from a string

I am a bit new to this site but I have looked an many possible answers to my question but none of them has answered my need. I have a feeling it's a good challenge. Here it goes.
In one of our tables we list what is used to run a report this can mean that we can have a short EXEC [svr1].[dbo].[stored_procedure] or "...From svr1.dbo.stored_procedure...".
My goal is to get the stored procedure name out of this string (column). I have tried to get the string between '[' and ']' but that breaks when there are no brackets. I have been at this for a few days and just can't seem to find a solution.
Any assistance you can provide is greatly appreciated.
Thank you in advance for entertaining this question.
almostanexpert
Considering the ending character of your sample sentences is space, or your sentences end without trailing ( whether space or any other character other than given samples ), and assuming you have no other dots before samples, the following would be a clean way which uses substring(), len(), charindex() and replace() together :
with t(str) as
(
select '[svr1].[dbo].[stored_procedure]' union all
select 'before svr1.dbo.stored_procedure someting more' union all
select 'abc before svr1.dbo.stored_procedure'
), t2(str) as
(
select replace(replace(str,'[',''),']','') from t
), t3(str) as
(
select substring(str,charindex('.',str)+1,len(str)) from t2
)
select
substring(
str,
charindex('.',str)+1,
case
when charindex(' ',str) > 0 then
charindex(' ',str)
else
len(str)
end - charindex('.',str)
) as "Result String"
from t3;
Result String
----------------
stored_procedure
stored_procedure
stored_procedure
Demo
With the variability of inputs you seem to have we will need to plan for a few scenarios. The below code assumes that there will be exactly two '.' characters before the stored_procedure, and that [stored_procedure] will either end the string or be followed by a space if the string continues.
SELECT TRIM('[' FROM TRIM(']' FROM --Trim brackets from final result if they exist
SUBSTR(column || ' ', --substr(string, start_pos, length), Space added in case proc name is end of str
INSTR(column || ' ', '.', 1, 2)+1, --start_pos: find second '.' and start 1 char after
INSTR(column || ' ', ' ', INSTR(column || ' ', '.', 1, 2), 1)-(INSTR(column || ' ', '.', 1, 2)+1))
-- Len: start after 2nd '.' and go until first space (subtract 2nd '.' index to get "Length")
))FROM TABLE;
Working from the middle out we'll start with using the SUBSTR function and concatenating a space to the end of the original string. This allows us to use a space to find the end of the stored_procedure even if it is the last piece of the string.
Next to find our starting position, we use INSTR to search for the second instance of the '.' and start 1 position after.
For the length argument, we find the index of the first space after that second '.' and then subtract that '.' index.
From here we have either [stored_procedure] or stored_procedure. Running the TRIM functions for each bracket will remove them if they exist, and if not will just return the name of the procedure.
Sample inputs based on above description:
'EXEC [svr1].[dbo].[stored_procedure]'
'EXEC [svr1].[dbo].[stored_procedure] FROM TEST'
'svr1.dbo.stored_procedure'
Note: This code is written for Oracle SQL but can be translated to mySQL using similar functions.

Orace SQL Case WHEN Statement "missing keyword"

I am using BI Publisher with Oracle Fusion 19c
I am trying to look at a 1000 word VARCHAR field (POH.NOTE_TO_VENDOR), then if it contains the word Hearing and the character after it (first part of the substr) are not blank ie a date exist. Then I want to convert the whole substr to a date other wise leave blank.
I can get the concatenated substr to work and display it as a string either as a date or if its not there then the value is '//' but cannot convert directly because if the value return '//' it wont convert this to date.
, CASE WHEN SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+14),2)IS NOT NULL
THEN (TO_DATE((SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+17),2)||'/'||SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+14),2)||'/'||SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+11),2)),'DD/MM/YY'))
ELSE
END
This code returns the error missing keyword. I want it to either convert the string to date or return a blank value.
Tried with an else condition without else and as it is
I have also tried Case when......then ‘1’ else ‘0’ which returns 1 and 0s correctly
The problem lies in this chaos of parentheses.
I removed all the unnecessary parentheses which make the code hard to read and hard to spot the syntactical errors:
, CASE WHEN SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 14, 2) IS NOT NULL
THEN TO_DATE(
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 17, 2)
|| '/' ||
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 14, 2)
|| '/' ||
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 11, 2),
'DD/MM/YY'
)
END
I also removed ELSE which is not needed if you only need NULL in that case.

Trim FIRST character in string that has multiple text of that character

I'm using SQL Server 2008 and I'm trying to trim values that looks like this
DocID
----------------
FOO_1_23_456
FOO1_1_23_4567
I'm trying to make it so it will only give me everything after the first '_'
Result
_1_23_456
_1_23_4567
Right now my query is
select
right(DocIDDocument, LEN(DocID.Document) - 3)) AS NewDocID
which only the trims the first 3 characters, I need it to where it trims everything before the first '_'
Thanks
Use stuff() and charindex():
select stuff(document, 1, charindex('_', document) - 1, '')