TRIM or REPLACE in Netsuite Saved Search - sql

I've looked at lots of examples for TRIM and REPLACE on the internet and for some reason I keep getting errors when I try.
I need to strip suffixes from my Netsuite item record names in a saved item search. There are three possible suffixes: -T, -D, -S. So I need to turn 24335-D into 24335, and 24335-S into 24335, and 24335-T into 24335.
Here's what I've tried and the errors I get:
Can you help me please? Note: I can't assume a specific character length of the starting string.
Use case: We already have a field on item records called Nickname with the suffixes stripped. But I've ran into cases where Nickname is incorrect compared to Name. Ex: Name is 24335-D but Nickname is 24331-D. I'm trying to build a saved search alert that tells me any time the Nickname does not equal suffix-stripped Name.
PS: is there anywhere I can pay for quick a la carte Netsuite saved search questions like this? I feel bad relying on free technical internet advice but I greatly appreciate any help you can give me!

You are including too much SQL - a formulae is like a single result field expression not a full statement so no FROM or AS. There is another place to set the result column/field name. One option here is Regex_replace().
REGEXP_REPLACE({name},'\-[TDS]$', '')
Regex meaning:
\- : a literal -
[TDS] : one of T D or S
$ : end of line/string
To compare fields a Formulae (Numeric) using a CASE statement can be useful as it makes it easy to compare the result to a number in a filter. A simple equal to 1 for example.
CASE WHEN {custitem_nickname} <> REGEXP_REPLACE({name},'\-[TDS]$', '') then 1 else 0 end

You are getting an error because TRIM can trim only one character : see oracle doc
https://docs.oracle.com/javadb/10.8.3.0/ref/rreftrimfunc.html (last example).
So try using something like this
TRIM(TRAILING '-' FROM TRIM(TRAILING 'D' FROM {entityid}))
And always keep in mind that saved searches are running as Oracle SQL queries so Oracle SQL documentation can help you understand how to use the available functions.

Related

Spaces in the name of substitutional variables

In oracle Sql, we need to use '&' indicate substitution variable.
If the variable name is comprised with multiple words and I want to spaces between them, what should I do ?
I can of course connect these words with "_" like "&aaa_bbb_ccc", but if I have to use space rather than "_" what should I do ?
You can't have spaces in the variable name. But the name shouldn't matter to anyone running the code, it just needs to be meaningful to whoever is writing and maintaining it.
I suspect whoever is running the code is seeing the name when they are prompted for the value, e.g. as a very simple example, if the code is:
select '&abc_def' from dual;
then a user running it sees:
Enter value for abc_def:
which isn't very friendly, and I can imagine you wanting to have spaces instead of underscores in that prompt - so perhaps that is what you really mean.
SQL *Plus (and SQL Developer, and SQLcl) lets you create your own prompt for a substitution variable value with the ACCEPT command`:
accept abc_def char prompt = 'Please give a value for Abc Def';
select '&abc_def' from dual;
Now the end user see:
Please give a value for Abc Def:
You probably want to set verify off if you aren't already,
Read more about using substituion variables.

Extract substring from character A to character B or EOL using Regexp_extract of Big Query / Google Analytics

I'm working with Google Big Query and try to extract some information from a string column into another column using Regexp_extract. In short:
Data in myVariable:
yippie/eggs-spam/?portlet:hungry=1234
yippie/eggs-spam/?portlet:hungry=456&portlet:hungrier=7890
I want a column with:
1234
456
My command:
SELECT Regexp_extract(myVariable, r'SOME_MAGIC') as result
FROM table
I tried for SOME_MAGIC:
hungry=(.*)[&$] - null, 456 (I learned that $ is interpreted as is)
hungry=(.*)(&|$) - Error: Exactly one capturing group must be specified
hungry=(.*)^& - null, null
hungry=(&.*)?$ - null, null
I read this, but there the number has a fixed length. Also looked at this, but "?=" is no known command for perl.
Does anybody have an idea? Thank you in advance!
I just found an answer to how I can solve my problem differently:
hungry=([0-9]+) - 1234, 456
It isn't an answer to my abstract question (regex for selecting Charater A to [Character B or EOL]), so it's not that satisfying. E.g. it won't work with
yippie/eggs-spam/?portlet:hungry=12AB34
However my original problem is solved. I leave the question open for a while in case somebody has a better answer.
I think I had a similar problem were I was trying to select the last 6 characters in a string (link_id) as a new column.
I kept getting this error:
Exactly one capturing group must be specified
My code originally was:
SELECT
...
REGEXP_EXTRACT(link_id, r'......$') AS updated_link_id
FROM sometable;
To get rid of the error and retrieve the correct substring as a column, I had to add parentheses around my regex string.
SELECT
...
REGEXP_EXTRACT(link_id, r'(......$)') AS updated_link_id
FROM sometable;

Oracle REGEXP search a pattern and do operation before replacement

I have data in Oracle 11g and have 2 scenarios to solve, can someone here suggest a solution
sample data in column: Name1-Value1+Name2-Value2+Name3-Value3a+ Value3b+Name4-
Value4a+Value4b + Value4c+Name5-Value5+
Note that '+' is a delimiter between names and also between sub-values (like in case of Value3a and value3b)
There are 2 problems to solve
Find sub-string(s) where + is a sub-value delimiter and replace it with space.
I tried but it doesn't work when there are more than one value separator as in this case Value4a+Value4b + Value4c)
REGEXP_REPLACE(column_value, '\+([^-]*\+)[^$]', \1)
Two + without a - in between tells me that they are sub-value
separator and that's what I am using here in pattern.
I have a 2-step approach to solve this problem but I am trying to see if a single statement (nested is fine) can solve it (in case something like below is doable)
REGEXP_REPLACE(column_value, '\+([^-]*\+)[^$]', Replace all + with space from \1)
Second is, is there a way that I can define 2 or more patterns to search
REGEXP_REPLACE(column_value, Pattern one or pattern2, replacement_string)
Thank you for reading the question

Cleaning SQL Data

What is causing the two fields to be different ? Is it a tab or something else ? What is an easy way to clean it ? I know I can somehow use replace, but I am unsure of what I am replacing and there are many more records with the same problem.
Name Binary
MCMPAD 0x4D0043004D00500041004400200020
MCMPAD  0x4D0043004D00500041004400A00020
SELECT Name , convert(binary(15), (Name)) Binary from VirtualTerminal
where Name like '%MCMPAD%'
One string ends with space-space 0x2020, the other with linefeed-space 0x0A20. The difference is not visible when you display Name as a string.
The extra zeroes (space is 0x0020 instead of 0x20) are for the Windows standard UCS-2 encoding.

SQL cannot search

In my SQL table Image, when i perform a search query
SELECT * FROM Image WHERE platename LIKE 'WDD 666'
it return no result(using other column to search then no problem).
The all the column data was inserted by C# code. (If enter data manually search works.)
now i suspect that the words WDD 666 wasn't english alphabet. is this possible?
In c#,
the plate number was generate by using tesseract wrapper string type.
what should i do to search the plate number?
Thanks in advance and sorry for my bad English.
Since your case matches, I'm going to rule out Case-sensitivity.
There may be leading or trailing blank spaces - Try this..
SELECT * FROM Image WHERE platename LIKE '%WDD 666%'
Try running this command:
SELECT '*'+plateName+'*',len(plateName)
FROM image.
I suspect platename has some non-printable characters in the field.
It appears to be a CR/LF at the end of the data. You can use
UPDATE image SET plateName = replace(plateName,char(13)+char(10),'')
WHERE plateName like '%'+char(13)+char(10)+'%'
If you get a positive row count, you'll know there was CR/LF data and it was removed. If you run the select afterwards, your lengths should be 7 and 8 based on your sample data