REPLACE function for replacing part of string in specific column - sql

I have written the following code:
SELECT *
FROM BMD_MI_OPS.DBH_TELEFONIE
WHERE cast(DATUM_TIJD as date) BETWEEN 1180212 AND 1180217;
UPDATE BMD_MI_OPS.DBH_TELEFONIE
SET QUEUE_NAAM = REPLACE(QUEUE_NAAM, '_DVB', '');
This should take all columns of the table BMD_MI_OPS.DBH_TELEFONIE within the given period in the WHERE statement. Then it should erase every _DVB that appears in the column QUEUE_NAAM. For example, VQ_PAR_EC_00_DVB should become VQ_PAR_EC_00.
I guess I am doing something wrong, any help on how to get this done would be appreciated.
Thanks in advance.

Your statements are not linked, if you want to update your data you need to add a WHERE clause in your UPDATE
For example :
UPDATE BMD_MI_OPS.DBH_TELEFONIE
SET QUEUE_NAAM = REPLACE(QUEUE_NAAM, '_DVB', '')
WHERE CAST(DATUM_TIJD AS DATE) BETWEEN 1180212 AND 1180217;
Selecting rows before your update has no impact on your update, it' just a SELECT

Related

How can I replace the first two characters of every row in a specific column using SQL?

I'm working with a table. In the table there is a column called ticket number which contains several rows of data. All of the values in the row begin with J2. I'd like to change the first two characters of all the rows to A3. How can I use SQL to do this. I'm familiar with the replace function:
SELECT REPLACE ([ticket number],'J2','A3')
But clearly the example above will not work, since it will change all of the J2 occurrences to A3 while I need to replace the first one at the beginning of ticket number. Any help would be appreciated.
Ticket Number
J2F4T45T
J2J3J3J2
J25TGYHJ2
J2FFJ2J2
J2MG8NGJ2
The desired result should be:
Ticket Number
A3F4T45T
A3J3J3J2
A35TGYHJ2
A3FFJ2J2
A3MG8NGJ2
Not sure if this is what you are looking for. But you could try to use a right function to get all but the 1st two character from the ticket_number:
SELECT 'A3' + RIGHT(ticket_number ,len(ticket_number)-2)
And if you need to update the table you could try something like this:
UPDATE ticket
set ticket_number = 'A3' + RIGHT(ticket_number ,len(ticket_number)-2)
db fiddle
This is also another method:
UPDATE ticket
set ticket_number = STUFF(ticket_number,1,2,'A3')

postgresql remove last 4 characters from text values

I have a table with only one column that has multiple text values. All of them end with same 4 characters that I would like to remove.
Could anyone, please, help me with a query for that?
I've already tried 'replace'
SELECT REPLACE(ticker, 'USDT', '') FROM tickers;
It appears to do what I want, but it doesn't update my data in a table.
You need an update statement
UPDATE tickers
SET ticker = REPLACE(ticker, 'USDT', '');
If you are not sure about the last 4 character, you can update using below as well -
UPDATE tickers
SET ticker = SUBSTR(ticker,-4);

Can I check if SQL REPLACE finds match in single query then update second field?

As title suggests if I were to run a replace query and it's successful, I then want to update a field in that same query if possible.
UPDATE users
SET solved = REPLACE(solved, ',testsolved123', '') AS match if match = true, SET found = found +1;
I realise this statement wouldn't work, I'm just trying to convey the logic I'm after, is there a case method?
From your description, you want to use a where clause:
UPDATE users
SET solved = REPLACE(solved, ',testsolved123', ''),
found = found + 1
WHERE solved like '%,testsolved123%';
I have no idea what the concat() is supposed to be doing.
It also seems like you are storing comma-delimited lists in a single string columns. That is a very, very bad idea.

Change string with sql UPDATE query

Question
How could I change the following code from SGC1-0001[ to SGC1-001.
The table is stations with column code.
I need to use an update query, but not sure how to use it.
There are more records in the column that differ slightly but I should be able to work it out with an example to do one.
Sample Data
I would like to do them in chunks though not one at a time.
SGC1-0001[
SGC1-0002[
..
SGC1-0019[
SGC1-0021[
SGC2-0001[
SGC2-0002[
..
SGC2-0016[
SGC2-0017[
SGC3-0003[
SGC3-0004[
...
SGC4-0018[
SGC4-0021[
SGC4-0022[
SGC4-0025[
SGC4-0029[
Logic (pseudo code)
Delete first 0 and last [ from all
If you're wanting to trim the last character of all records in that column as well as remove the leading 0, the following should do the trick:
UPDATE stations
SET code = REPLACE(LEFT(code, LEN(code) - 1), '-0', '-')
If you're just wanting to replace the leading 0, following should do:
UPDATE stations
SET code = REPLACE(code, '-0', '-')
Also, the following may be of interest:
String Functions
Try This, Update query
Because all your code have '[' at last then you can use this query. it will delete the last '[' and first 0 after '-' from all records
UPDATE stations
SET CODE = LEFT(replace(code,right(code,1),''),4) + '-'+
RIGHT(RIGHT(replace(code,right(code,1),''),4),3)
It will replace
SGC1-0029[ to SGC1-029 AND
SGC1-0001[ to SGC1-001
Better to refer some sql basic tutorials

SELECT a cell and UPDATE at the same time?

I've got a SQL Server table in which I have a column where I would like to select the current value and increment by one, is there a way to do this in a single query? This in order to mitigate the chance, however small it might be, that someone else gets the same number.
Something along the lines of this pseudo code:
SELECT NumSeriesCurrent
FROM NumSeries
(UPDATE NumSeries SET NumSeriesCurrent = NumSeriesCurrent+1)
WHERE NumSeriesKey='X'
To update the value and get the value in NumSeriesCurrent previous to the update you can use
UPDATE NumSeries
SET NumSeriesCurrent += 1
OUTPUT DELETED.NumSeriesCurrent
WHERE NumSeriesKey='X'