sql update part string and trim trailing value - sql

I am trying to update a string but I also need to wipe out the rest of the values following the found string. I can't use the replace since the value at the end will change but the middle part will stay the same for many records. I haven't seen a post for removing the trailing portion of a string if you don't know it's exact value or location within the string.
I am using Oracle for my database thru SQL Developer to update the data.
"keep this data" "search on keyword" "wipe out trailing data" "result data"
xyz # psu.edu xyz
Column data value = xyz#psu.edu
I would search for a record with columnname like '#%'
Remove "#%"
End result value = xyz
Column data value = ABCabc123
I would search for a record with columnname like 'abc%'
Remove "abc%"
End result value = ABC
I have not seen any answers with this type of update. Please help!

select substr('xyz#psu.edu',1, instr('xyz#psu.edu','#') - 1) from dual
select substr('ABCabc123',1, instr('ABCabc123','abc') - 1) from dual

You might need to check in case the string you want is not found:
SELECT CASE
WHEN INSTR('xyz#psu.edu','#', 1, 1) > 0
THEN SUBSTR('xyz#psu.edu',1,INSTR('xyz#psu.edu','#', 1, 1)-1)
ELSE 'xyz#psu.edu'
END as "Result Data"
FROM DUAL

Related

I want to update a string in a table

I have a table, content_history with a column, doc_filename, and each row has a string value. Each string has a | and the portion in front of the | is a filepath. I want to replace that portion of the string with the correct filepath if it doesn't already match. What is the best way to go about doing this? Currently I use:
UPDATE content_history
SET doc_filename = replace (doc_filename, 'path that needs to be replaced', 'new path')
WHERE doc_filename LIKE 'old path%'
But if I don't have the exact path it doesn't replace so I have to run a select * query and manually go through and input all the different paths that are incorrect. It's not a viable long-term solution
Ideally you wouldn't store multiple values as delimited values in a single value, you should have a separate column for each distinct value, then you wouldn't be asking such a question.
You can use stuff:
set doc_filename=Stuff(doc_filename, 1, CharIndex('|', doc_filename)-1, 'new path')
you need to split value to avoid an incorrect update
update CH
set CH.doc_filename = 'new_path' + '|' + P.right_part
from content_history CH
outer apply
(
select left(CH.doc_filename, charindex('|', CH.doc_filename) - 1) as left_part
,right(CH.doc_filename, len(CH.doc_filename) - charindex('|', CH.doc_filename)) as right_part
where charindex('|', CH.doc_filename) != 0
) P
where P.left_part = 'old_path'

How to ignore specific string value when using pattern and patindex function in SQL Server Query?

I have this query here.
WITH Cte_Reverse
AS (
SELECT CASE PATINDEX('%[^0-9.- ]%', REVERSE(EmailName))
WHEN 0
THEN REVERSE(EmailName)
ELSE left(REVERSE(EmailName), PATINDEX('%[^0-9.- ]%', REVERSE(EmailName)) - 1)
END AS Platform_Campaign_ID,
EmailName
FROM [Arrakis].[xtemp].[Stage_SendJobs_Marketing]
)
SELECT REVERSE(Platform_Campaign_ID) AS Platform_Campaign_ID, EmailName
FROM Cte_Reverse
WHERE REVERSE(Platform_Campaign_ID) <> '2020'
AND REVERSE(Platform_Campaign_ID) <> ''
AND LEN(REVERSE(Platform_Campaign_ID)) = 4;
It is working for the most part, below is a screenshot of the result set.
The query I posted above extracts the 4 numbers to the right out of the initial value that is set for the column I am extracting out of. But I am unable to figure out how I can also have the query ignore cases when the right most value is -v2, -v1, etc. essentially anything with -v and whatever number version it is.
If you want four digits, then one method is:
select substring(emailname, patindex('%[0-9][0-9][0-9][0-9]%', emailname), 4)

SQL: How to make a replace on the field ''

I have a very but tricky question for you guys. So, listen I have a field with spaces and numbers in one of my table columns. The key part is transform the content in a decimal field. The drawback is basically that for some rows I could get something like:
' 1584.00 '
' 156546'
'545.00 '
' '
So, to clean up my column, I have done a LTRIM and RTRIM so spaces gone. So now for a couple of records where the record were just spaces the new content is ''. Finally I need to convert this result to a decimal.
Issue: The thing is that for field that contend just the spaces the new result is '' and I'm not able to apply a REPLACE on this because it's a blank and the code below doesn't work:
SELECT REPLACE('','','0')
-- Final current verison
SELECT CAST(COALESCE(REPLACE(REPLACE([Gross_Weight],' ','0'),',',''),'0') AS DECIMAL(13,3))
How could I figure it out?
thanks so much
SELECT COALESCE(NULLIF(MyColumn, ''), 0)
This has the side-effect that you will also turn NULL values into 0, which you might not want. If that's a problem then a simple CASE statement should do the trick:
SELECT CASE WHEN MyColumn = '' THEN 0 ELSE CAST(MyColumn AS DECIMAL(10, 4)) END
Obviously you'll also have to incorporate any other manipulations that you're already doing.
No need for replace, just concatenate a zero to your column, like
SELECT RTRIM('0' + LTRIM(column))
I presume your data is in a table.
Lets call this table 'DATA' and the column 'VALUE'
Then you might use the below query
UPDATE DATA SET VALUE = 0 where VALUE = ''
To select the value do the below
select case ltrim(rtrim([Gross_Weight])) when ''
THEN 0
ELSE ltrim(rtrim([Gross_Weight])) END
Let me know if i get the requirement wrong.

How to fetch last character and increment it in sql

I have the following query
select case
when Right('BPUREN_3',1) Like '[A-Z]'
then concat('BPUREN','_1')
else
concat('BPUREN_3',right('BPUREN_3',1)+1)
End
I want output as BPUREN_4 But I am getting the result as BPUREN_34
select case
when Right('BPUREN_3',1) Like '[A-Z]' then 'BPUREN_1'
else concat(Left('BPUREN_3', 7),right('BPUREN_3',1)+1)
End
No need to concat('BPUREN','_1') it will always be 'BPUREN_1'
You could change Left('BPUREN_3', 7) to Left('BPUREN_3', LEN('BPUREN_3') - 1) if 'BPUREN_3' could change in length (for example if its a parameter in your real code)
concat('BPUREN_3', right('BPUREN_3', 1)+1) -- concat combines two strings together and returns a string. So you are combining BPUREN_3 with 4 (outputting the BPUREN_34). Instead change BUREN_3 toBPUREN_

How do identify the first character of a string as numeric or character in SQL

I need to identify the first character in my data as numeric or character in SQL Server. I am relatively new to this and I don't know where to begin on this one. But here is what I have done to this point. I had data that looked like this:
TypeDep
Transfer From 4Z2
Transfer From BZZ
Transfer From 123
Transfer From abc
I used the right function to remove the 'transfer from' and isolate the data I need to check.
UPDATE #decode
SET firstPartType = Right(z.TypeDep,17)
FROM #decode z
where z.TypeDep like 'TRANSFER FROM%'
firstPartType
4Z2
BZZ
123
abc
Now I need to add a column identifying the first character in the string. Producing the results below.
firstPartType SecondPartType
4Z2 Numeric
BZZ Alpha
123 Numeric
abc Alpha
Using LEFT and ISNUMERIC(), however be aware that ISNUMERIC thinks some additional characters such as . are numeric
UPDATE #decode
SET SecondPartType =
CASE WHEN ISNUMERIC(LEFT(firstPartType, 1)) = 1 THEN'Numeric'
ELSE 'Alpha'
END
FROM #decode;
A more robust approach is to use the limited regex functionality of sql server. ISNUMERIC will return false positives for single characters like .,$ to name a few.
SELECT
CASE WHEN left(firstPartType, 1) like '[0-9]' THEN 'Numeric'
ELSE 'Alpha'
END AS SecondPartType
I think this should work:
SELECT
CASE WHEN ISNUMERIC(SUBSTRING(firstPartType, 1, 1)) = 1
THEN 'Numeric'
ELSE 'Alpha'
END AS 'SecondPartType'
FROM TABLE
you can use this command
ISNUMERIC(LEFT(firstPartType, 1))
this return 1 if the first character is a Numbert
0 if isn't.
i think is all you need
You could try:
UPDATE #decode
SET SecondPartType =
CASE
WHEN LEFT(firstPartType, 1) IN ('0','1','2','3','4','5','6','7','8','9')
THEN'Numeric'
ELSE 'Alpha'
END
FROM #decode;
select ISNUMERIC(left('4ello world',1)) will be a "1" if the first character is a number.