SQL snowflake: case when with wildcard returning everything with the wrong value - snowflake-cloud-data-platform

The following code is returning "Control" in the column CELL instead of "Test". What am I missing?
Select
NAME,
case
when NAME like '%_A_%' then 'Control'
when NAME like '%_B_%' then 'Test'
else 'N/A'
end as CELL
from table1
NAME
CELL
D119992_A64938_email_SUBMIT_kjsdfhksuere8rw987r9_B_Send1
Control
I have tied re-ordering the case when and adding different conditions but it keeps returning Control instead of Test.

Underscore has a special meaning when used with LIKE, and it means any one single character. Since you seem to want to match literal underscore, you may escape it:
SELECT NAME,
CASE WHEN NAME LIKE '%\\_A\\_%' ESCAPE '\\' THEN 'Control'
WHEN NAME LIKE '%\\_B\\_%' ESCAPE '\\' THEN 'Test'
ELSE 'N/A'
END AS CELL
FROM table1;

Might look cleaner with rlike
when NAME rlike '.*_A_.*' then 'Control'
when NAME rlike '.*_B_.*' then 'Test'

Related

How can I pull out multiple substrings from within a single string

For example, I have this string in an SSMS DB:
Sent for processing:1 ;DK A/C-WestlySnipes:ACCT NOT FOUND ;DK A/C-SonyaBlade:ACCT NOT FOUND
What I want to be able to do is pull out WestleySnipes and SonyaBlade from within that string and store it in a temp table. The names can be different and there can be more than one name within a particular string.
I've tried using substrings combined with CharIndex but I can only pull out 1 value. Not sure how to pull out multiple names from within the same string where the names can change in any given string.
In SQL Server 2016 and later, STRING_SPLIT(string, delimiter) is a table valued function. You can split your text string with a ; delimiter like so. (fiddle).
SELECT value FROM STRING_SPLIT(
'Sent for processing:1 ;DK A/C-WestlySnipes:ACCT NOT FOUND ;DK A/C-SonyaBlade:ACCT NOT FOUND',
';')
You get back this:
value
Sent for processing:1
DK A/C-WestlySnipes:ACCT NOT FOUND
DK A/C-SonyaBlade:ACCT NOT FOUND
Then you can use ordinary string-editing functions on value in your query to extract the precise substrings you need. Something like this will work for your specific example. (fiddle.)
SELECT
value,
REPLACE(REPLACE(value, 'DK A/C-', ''), ':ACCT NOT FOUND', '') val
FROM STRING_SPLIT(
'Sent for processing:1 ;DK A/C-WestlySnipes:ACCT NOT FOUND ;DK A/C-SonyaBlade:ACCT NOT FOUND',
';')
WHERE value LIKE 'DK %'

How do I remove specific words from a Netezza string?

I want to be able to remove specific words from my EARNINGS_CODE column. The words I need to remove are "Earnings" and "Results". I tried using the translate function but that just replaced every letter, not the exact word.
SELECT TRANSLATE(EARNINGS_CODE,'EARNINGS','')
FROM PROD_SRC..UWH_OCS_HCM_PAYROLL_COSTING_BIWEEKLY
Try
select
regexp_replace(earnings_code, 'EARNINGS', '')
from ....
For more things to replace, just add to the regex
select
regexp_replace(earnings_code, 'EARNINGS|foo|bar|foobar', '')
from ....

How can I add a dash to separate the string when is too long in SQL Server? I need to write the script, not hardcoding it...thanks

I need to rename my records..so the data the I have today is like:
user.apple
user.applebanana
user.applegrapes
user.applegrapeskiwi
I want:
user.apple,
user.apple-banana,
user.apple-grapes,
user.apple-grapes-kiwi
and so on...
I need to add a dash when the second string is too big...
I'm newbie in SQL, I have tried a substring but is not this. It would be easier if I could use a replace() function, but I'm not supposed to hardcode... :(
If the second word is always 'apple' and is always prefixed with . then you could use STUFF to inject the character:
SELECT YourColumn,
CASE LEN(YourColumn) WHEN CHARINDEX('.',YourColumn) + 5 THEN YourColumn
ELSE STUFF(YourColumn, CHARINDEX('.',YourColumn)+6,0,'-') END
FROM (VALUES('user.apple'),
('user.applebanana'),
('user.applegrape'),
('operator.apple'),
('operator.applekiwi'))V(YourColumn);
The CASE expression is there, as otherwise you'd have 'user.apple-'.
DB<>Fiddle

SQL Server - REPLACE - Matching string with old substring edits entire field?

I recently had a request come through to remove some Agent names from the guest surname field in a client's database.
Eg. 'John Smith -Wotif'
When testing using the following UPDATE statement, the entire field was wiped rather than just the specific string.
UPDATE GUEST
SET SURNAME = REPLACE(' -Wotif',' -Wotif','')
WHERE SURNAME LIKE '% -Wotif'
I've since found that simply using the column name as the matching string will allow the full statement to work (even if already specified in the SET section), but I can't work out where the logic of the original statement effectively says 'wipe these fields entirely'.
Unless specified otherwise, surely the '' replacement only applies to the value contained within the substring, regardless of whether the string and substring match?
The first argument in the REPLACE function is the full string that you want to search. So you should be referencing the SURNAME field rather than specifying part of the string.
REPLACE(SURNAME,' -Wotif','')
You update SQL command should be like this -
UPDATE GUEST
SET SURNAME = REPLACE(SURNAME, 'FindValue' , 'ReplaceWithValue')
WHERE SURNAME LIKE '% -Wotif'
If you want to find & replace '-Wotif' with blank, then update command should be like below-
UPDATE GUEST
SET SURNAME = REPLACE(SURNAME, '-Wotif' , '')
WHERE SURNAME LIKE '% -Wotif'

Building dynamic query for Sql Server 2008 when table name contains " ' "

I need to fetch Table's TOP_PK, IDENT_CURRENT, IDENT_INCR, IDENT_SEED for which i am building dynamic query as below:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{1}]') AS CURRENT_IDENT, IDENT_INCR('[{1}]') AS IDENT_ICREMENT, IDENT_SEED('[{1}]') AS IDENT_SEED", pPrimaryKey, pTableName)
Here pPrimaryKey is name of Table's primary key column and pTableName is name of Table.
Now, i am facing problem when Table_Name contains " ' " character.(For Ex. KIN'1)
When i am using above logic and building query it would be as below:
SELECT (SELECT TOP 1 [ID] FROM [KIL'1]) AS TOP_PK, IDENT_CURRENT('[KIL'1]') AS CURRENT_IDENT, IDENT_INCR('[KIL'1]') AS IDENT_ICREMENT, IDENT_SEED('[KIL'1]') AS IDENT_SEED
Here, by executing above query i am getting error as below:
Incorrect syntax near '1'.
Unclosed quotation mark after the character string ') AS IDENT_SEED'.
So, can anyone please show me the best way to solve this problem?
Escape a single quote by doubling it: KIL'1 becomes KIL''1.
If a string already has adjacent single quotes, two becomes four, or four becomes eight... it can get a little hard to read, but it works :)
Using string methods from .NET, your statement could be:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{2}]') AS CURRENT_IDENT, IDENT_INCR('[{2}]') AS IDENT_ICREMENT, IDENT_SEED('[{2}]') AS IDENT_SEED", pPrimaryKey, pTableName, pTableName.Replace("'","''"))
EDIT:
Note that the string replace is now only on a new, third substitution string. (I've taken out the string replace for pPrimaryKey, and for the first occurrence of pTableName.) So now, single quotes are only doubled, when they will be within other single quotes.
You need to replace every single quote into two single quotes http://beyondrelational.com/modules/2/blogs/70/posts/10827/understanding-single-quotes.aspx

Resources