Change string with sql UPDATE query - sql

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

Related

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);

REPLACE function for replacing part of string in specific column

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

Removing last character in ACCESS if it is "."

I am trying to write an update query that will remove the last character if it is a period ("."). In EXCEL, I would use the following statement:
=IF(RIGHT(A1,1)=".",LEFT(A1,LEN(A1)-1),A1)
How can I modify this for ACCESS? I found the following statement in a forum, but ACCESS seems to have a problem with "Substring" and won't let me run the query.
UPDATE table SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)WHERE field LIKE '%.'
Any thoughts?
I think the right way to do this in Access is:
UPDATE table
SET field = LEFT(field, LEN(field) - 1)
WHERE field LIKE '*.' ;
Note that the like wildcards are different in MS Access.
You could simply create a substring that was one character shorter than your existing string if it ended with a period via the LEFT() function :
UPDATE YourTable
SET YourColumn = LEFT(YourColumn, LEN(YourColumn - 1))
WHERE YourColumn LIKE '*.'

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.

SQL Statement to UPDATE three characters in a string

How to take a string from a row in a column named 'link', and modify it by adding three letters to a specific index position in the string.
Specific example:
I want to select the value 'http://www.hello.no' and UPDATE it with 'http://www-x1.hello.no' using SQL statement(s).
Lets say the index position where '-x1' starts at will always be 10.
This needs to be accomplished using PostgreSQL. But if you can capture the logic with a generic SQL statement then great. :)
Postgresql has a function for doing replacements with patterns called regexp_replace. You can use that function like this:
UPDATE my_table
SET link = regexp_replace(link, 'www', 'www-x1')
WHERE <...>
Of course you could do it with the straight string manipulation, too:
UPDATE my_table
SET link = left(link, 10) || '-x1' || substring(link from 10)
WHERE <...>
This does what you ask for:
update the_table
set the_column = left(the_column, 10) || '-x1' || substring(the_column, 10);
however I'm not sure that this is what you want. It seems you want to insert the '-x1' in front of the first . which would be something different.