How do I remove specific words from a Netezza string? - netezza

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 ....

Related

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'

SQL Server Full text search Contains function

İ have a problem with contains function, when i search with like '%ZAM%' operator, it finds all word that contains ZAM like ZAMANLAMA AZAMI ZAM and etc.. but when I use fts index contains function, it just find ZAM ZAMANLAMA but it doesnt find AZAMI or 123ZAM789. I have also tried CONTAINS (YourColumn, ' "ZAM" ' ) but it doesn't work. Please help me , fts is very fast but it could not find all contains like '%%' operator what should I do ?
You can use "*" before in contain syntax same as like operator . but you need to use double quote before and after the search string.
Try this query once.
SELECT *
FROM YourTable
WHERE CONTAINS(YourColumn,'"*ZAM*"');
(OR)
select * from YourTable where YourColumn like '%ZAM%'

TSQL - Replace string before symbol

I have a table storing pathes to files on sql server. I need to replace the path before the last backslash:
C:\Users\APP\AppData\Local\Temp\test\abc deg.pdf
to for example:
\app\pp\abc deg.pdf
EDIT: The table containts many pathes - I need to run through the whole table and change all pathes.
You can use:
CHARINDEX('\', REVERSE(#str))
to get the index of the first backslash starting from the end.
Using RIGHT with this index you can extract the string after the last backslash and concatenate it to the new path:
DECLARE #str VARCHAR(50) = 'C:\Users\APP\AppData\Local\Temp\test\abc deg.pdf'
SELECT '\app\pp' + RIGHT(#str, CHARINDEX('\', REVERSE(#str)))
Reverse the input string (using REVERSE) and find the index of the first backslash (using CHARINDEX).
Take the left part up to that index (using LEFT) and concatenate with the reverse of your replacement string (using + operator).
Then reverse that to get your final result.
Try this:
declare #a varchar(max)='C:\Users\APP\AppData\Local\Temp\test\abc deg.pdf'
select REPLACE(#a,SUBSTRING(#a,1,(LEN(#a)-charindex('\',reverse(#a),1))),'\app\pp')
Update: For updatingall the table column values.
select REPLACE([column-name],SUBSTRING([column-name],1,(LEN([column-name])-charindex('\',reverse([column-name]),1))),'\app\pp')
FROM [Your-table]
This is how it can be done.
UPDATE TABLE
SET PATH = REPLACE(PATH, 'C:\Users\APP\AppData\Local\Temp\test', '\app\pp')
WHERE ...
This is going to replace 'C:\Users\APP\AppData\Local\Temp\test' with '\app\pp'. Or you can modify the path as required.
Please test before executing this UPDATE statement. I havent specified filters here

Trim Spaces in a String

UserId should be like 'keerthi.ks' in the database.
But in some tables some data is like 'keerthi .ks'
Need to clear the middle spaces.
Please help me with this.
You simply need to replace the space by nothing.
REPLACE ( string_expression , string_pattern , string_replacement )
More info here:
https://msdn.microsoft.com/en-us/library/ms186862.aspx
Try this
SELECT REPLACE('keerthi .ks',' ','')
Note, this will remove all the space in the text
Simply use
SELECT REPLACE('keerthi .ks', ' ', '')
You can also use column name instead of hard coded value.

Resources