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);
Related
So assume there are 2 cols Name and Flag. I want to change the name of the data which have flag=1.
This is the table (https://i.stack.imgur.com/LILzV.png)
And this is what i want.(https://i.stack.imgur.com/JPZWj.png)
I have first created a flag according to some condition. Now im not able to fig out the next move.
update TableName
set name = 'zumba'
where flag = 1
Edit: as pointed out by jarlh, single quotes are the correct syntax
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')
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
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
I have a db where one column contains 2 pieces of data, e.g. first and last name.
The format is roughly ABC-1D23-4F34
I want to copy and insert the first 3 letters, the ABC, into a new column. Lets call these columns [full_id] and [ref_id]
From reading it looks like substring is able to do this but I am doing something wrong here.
INSERT INTO [ref_id]
SUBSTRING([full_id], 1, 3)
FROM db.Name
Thank you for the help.
EDIT:
The update string worked. But I found that there are issues with my data and it is not all in proper formatting.
Is there a way to write a case where if the substring is not 3 letters it writes a null value?
Thanks again, and sorry for having bad data.
Try
UPDATE Name
SET ref_id = CASE WHEN CHARINDEX('-',full_id) = 4 THEN SUBSTRING(full_id,1,3) ELSE NULL END
That will set the ref_id column for all rows using the first 3 characters of the full_id column.
If it is a column in the same table you need to switch to an update statement.
UPDATE db.Name SET [ref_id] = SUBSTRING([full_id], 1, 3)
Perhaps you want something like:
Update db.Name set ref_id = substring([full_id], 1,3)