How to trim a string which changes at each result - sql

I have data which contains a date and a batch number. I need to be able to trim down the batch number removing the leading word "Job " from each. The issue i have is each result is different and is of a different length also.
To try and deal with this i have tried to use LEFT and CHARINDEX to trim it but get a syntax error back. Because i am using MS Query on a open edge v10 progress odbc database it is not clear as to what the issue is. Below is the code i have produced.
SELECT
Delivery_0.DelProposedDate
, Delivery_0.DelBatchNumber
, LEFT(Delivery_0.DelBatchNumber,CHARINDEX(' ',Delivery_0.DelBatchNumber)-1) as 'JobID'
FROM SBS.PUB.Delivery Delivery_0
Currently the data looks like this:
DelProposedDate DelBatchNumber
05/05/2017 Job 321924
08/02/2019 Job 356812/4
29/03/2017 Job 328585
I am trying to get it to look like this:
DelProposedDate DelBatchNumber JobID
05/05/2017 Job 321924 321924
08/02/2019 Job 356812/4 356812/4
29/03/2017 Job 328585 328585

You want to exclude the left-most 4 characters ('Job '). This is the same a showing the right-most x characters where x = length-of-string - 4. I'm not that conversant with Progress' variant of SQL, but something like:
Right(DelBatchNumber, Len(DelBatchNumber) - 4)
would do it. You may need to substitute the Progress equivalent of Right and Len, and possibly check the order of the parameters Right takes.

SQL Server has a function that explicitly does this, STUFF():
select stuff(Delivery_0.DelBatchNumber, 1, 4, '')
This replaces the first four characters with an empty string.

try
select replace ('Job 321924','Job ','')
output
'321924'
first parameter is the string you want to change,
second parameter is the string you want to replace,
third parameter is the string you want to replace with.
So here i simply replaced 'Job ' by an empty string
Note that it returns a string and not an integer, you might need to use CONVERT/CAST if you want the result as integer

You can try this, which tries to replace the 'Job ' string with empty string:
SELECT
Delivery_0.DelProposedDate
, Delivery_0.DelBatchNumber
, REPLACE(Delivery_0.DelBatchNumber,'Job ','') as 'JobID'
FROM SBS.PUB.Delivery Delivery_0

Related

Oracle RegEx in a Cast Procedure

I have a Cast Procedure for a table with "raw" data. Any time a record comes from any of our locations into the raw table, my procedure "cleans" the data and loads it into a new table. The original raw table is all varchars and my procedure converts date and number fields to the proper data types. From the clean table, a Java program selects any new records on a daily basis and FTPs them off in a file to another dept. Have just learned that a few of the fields accept input from users and on a rare occasion, someone uses a pipe in what they input. A pipe symbol happens to be the delimiter that the other dept is using and whenever a pipe shows up in the middle of a field, it throws a wrench on their end.
I've never used REGEX or REGEXP_REPLACE in Oracle before. There are only three fields where the users can input data - MISTINTCOMMENT, PALETTE, COLORID. How do I use REGEX or REGEXP_REPLACE to replace any pipes with a space? Do I want to do it on each field? Or is this something I should "wrap around" the entire statement (in case there's a field I missed where someone might be able to input a pipe)?
Here is the portion of the procedure where the Values are cleaned and inserted into new table. How to best use RegEx with this?
VALUES (CASE
WHEN THECOSTCENTER IS NOT NULL
THEN THECOSTCENTER
ELSE (SUBSTR(TRIM(THESENDING_QMGR), -6))
END,
CASE
WHEN THESTORENBR = '0' AND (SUBSTR(THESENDING_QMGR, 1, 5) = 'PDPOS')
THEN TO_NUMBER(SUBSTR(THESENDING_QMGR, 8, 4))
WHEN THESTORENBR = '0' AND (SUBSTR(THESENDING_QMGR, 1, 8) = 'PROD_POS')
THEN TO_NUMBER(SUBSTR(THESENDING_QMGR, 9, 4))
ELSE TO_NUMBER(NVL(THESTORENBR,'0'))
END,
TO_NUMBER(NVL(THECONTROLNBR,'0')), TO_NUMBER(NVL(THELINENBR,'0')), THESALESNBR, TO_NUMBER(NVL(THEQTYMISTINT,'0')), THEREASONCODE, THEMISTINTCOMMENT,
THESIZECODE, THETINTERMODEL, THETINTERSERIALNBR, TO_NUMBER(NVL(THEEMPNBR,'0')), TO_DATE(THETRANDATE,'YYYY-MM-DD'), THETRANTIME, THECDSADLFLD,
THEPRODNBR, THEPALETTE, THECOLORID, TO_DATE(THEINITTRANDATE,'YYYY-MM-DD'), TO_NUMBER(NVL(THEGALLONSMISTINTED,'0'),'999999999.99'), THEUPDATEEMPNBR,
TO_DATE(THEUPDATETRANDATE,'YYYY-MM-DD'), TO_NUMBER(NVL(THEGALLONS,'0'),'999999999.99'), THEFORMSOURCE, THEUPDATETRANTIME, THESOURCEIND,
TO_DATE(THECANCELDATE,'YYYY-MM-DD'), THECOLORTYPE, TO_NUMBER(NVL(THECANCELEMPNBR,'0')), TO_BOOLEAN(THENEEDEXTRACTED), TO_BOOLEAN(THEMISTINTMQXTR),
THEDATASOURCE, THETRANGUID, TO_NUMBER(NVL(THETERMNBR,'0')), TO_NUMBER(NVL(THETRANNBR,'0')), TO_NUMBER(NVL(THETRANID,'0')), THEID, THETINTABLESALESNBR,
TO_NUMBER(NVL(THERETURNQTY,'0')), THECREATED_TS, THEXMIT_GUID, THESENDING_QMGR, THEMSG_ID, THEPUT_TS,
THEBROKER_NAME, THECHECKSUM);
If you have to use a REGEXP_REPLACE to replace pipes, escape them:
REGEXP_REPLACE(x, '\|', ' ')
This is useful to know when your more complex expressions include a pipe.
In this case, REPLACE that performs literal text search and replace will suffice:
REPLACE(x, '|', ' ')

Check if a string has a combination of a substring and numbers in sql

how do I write a SQL where statement that checks if a string contains some substring and a number. For example:
string: macsea01
where string like 'macsea' plus a number
Regex is the most obvious solution to this question. Without more detail about the specific format of the string, I can suggest the following, which will match a sequence of a letter in the alphabet followed immediately by a digit:
where column_name like '%[a-zA-Z][0-9]%'
If you're literally looking for macsea at the beginning of the string followed by a digit, it would be:
where column_name like 'macsea[0-9]%'
Regex seem to bee a little slippery here, depending on your needs you can for instance divide the string into several parts, first the text part, and take the rest of the string, try to convert it into a number.
Somthing like this (but I think this perticular code is broken
where substring(column_name, 1, 6) = 'macsea' and cast(substring(column_name, 7, 1000) as int) > 0

Replacing characters in the oracle database value range

I have a database in an oracle, I filled in the "Number" fields with numbers starting from "A14602727" to "A14603000" but it turned out that I accidentally typed the symbol A (in Ukrainian) instead of A (in English). And now I when find the command:
select number from test where number 'А14602727'
...find me nothing. Is it possible somehow with the help of the command to replace all numbers from "A14602727" (Ukrainian) to "A14602727" (English) ?
I will be grateful for your help!)
You can use following trick to convert any character to its base character using accent-insesitive binary sorting.
select your_col,
utl_raw.cast_to_varchar2(nlssort(your_col, 'nls_sort=binary_ai')) converted_col
from your_table
Use it in update statement accordingly.
Cheers!!
You could use regexp_replace():
update test set number = regexp_replace(number, '^Ä', 'A');
Regexp '^Ä' represents character 'Ä' at the beginning of the string. I used 'Ä' to represent the A in Ukrainian: replace this with the correct character that you want to replace.
This can also be done, probably more efficiently, with substr and like:
update test set number = 'A' || substr(number, 2) where number like 'Ä%';
Use the simple Oracle REPLACE function:
UPDATE test
SET number = REPLACE(number, 'A', 'A');
Replace is taking 3 parameters:
String in which you will search for a string to replace (The number column)
The string you are searchin gto replace (Ukrainian A)
The string you will replace it with (English A)
Here you have a DEMO example.
Also please note that the query in your question:
select number from test where number 'А14602727';
Is not valid. It should be something like this:
select number from test where number like 'А14602727';
or like this:
select number from test where number = 'А14602727';
So first do check that!
Cheers!

How to determine if a variable has a special character using dual table on sql?

I'm currently trying to check if a string has a special character (value that is not 0 to 9 A to Z a to z), but the inhouse language that I'm currently using has a very limited function to do it (possible but it will take a lot of lines). but I am able to do a query on sql. Now I would like to ask if it is possible to query using the dual table on sql, My plan is to pass the string to variable and this variable will be use on my sql command. Thanks in advance.
Here is what you can use
SELECT REGEXP_INSTR('Test!ing','[^[:alnum:]]') FROM dual;
This will return a number other than 0 whenever your string has anything other than letters or numbers.
You can use TRANSLATE to remove all okay characters from the string. You get back a string containing only undesired characters - or an empty string when there are none.
select translate(
'AbcDefg1234%99.26éXYZ', -- your string
'.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'.') from dual;
returns: %.é

split string with character

using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder