insert substring into new column - sql

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)

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

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

Sql query to add Zero between string with character and digits

What will be the generic sql query to update id by adding zeros in id. containing alphabets as well as numbers? In between the character and digits 00 needs to be padded e.g QTX23675 should turn into QTX0023675
UPDATE Table
SET QID = 'QTX0023675'
WHERE QID='QTX23675';
There are many records to update and i found query with leading zeros.
Tried with spliting the string.
In most databases, you could do something like this:
UPDATE Table
SET QID = CONCAT(LEFT('QTX0023675', 3), '00', RIGHT('QTX0023675', 5))
WHERE QID LIKE '________';
In any given database, there might be somewhat simpler methods, but the idea is the same.
Try This to make the result Dynamic
WITH CTE
AS
(
SELECT Val = 'XTP231'
)
SELECT
*,
NewVal = SUBSTRING(Val,1,PATINDEX('%[0-9]%',Val)-1)+'00'+SUBSTRING(Val,PATINDEX('%[0-9]%',Val),LEN(Val))
FROM CTE
THe Output is as follows
UPDATE TABLE SET QID= STUFF(QID,PATINDEX('%[0-9]%',QID),0,'00')

How to remove the comma seperated part of the column record from the oracle table

We have the oracle table called "Names" which has approximately 10k records. The table has one of the column as "ID". Now this column is totally messed up and the records looks somewhat similar to the following.
ID
A.011
A.012
A.013
B.014
B.015
B.016
A-B-C
X_Y_Z
randomName
OtherName
Now, what we have decided is to remove the dot(.) seperated portion of the record and leave the other records as is for now. So the record should look like
ID
011
012
013
014
015
016
A-B-C
X_Y_Z
randomName
OtherName
You could use this statement:
update names set id=regexp_replace(id,'^[^\.]+\.','')
as you see here, ids without . are simply untouched:
select regexp_replace('tst234','^[^\.]+\.','') from dual
and here a test with a .
select regexp_replace('A.0234','^[^\.]+\.','') from dual
use regular expression:
update names set id = case
when instr(id,'.') between 0 and length(id) - 1 then
regexp_replace(id,'^[^\.]*\.(.+)$','\1')
else id
end;
To UPDATE use this simple query:
UPDATE Name
SET ID = SUBSTR(ID, INSTR(ID, '.')+1);
See SQL Fiddle
P.S. Although it's kinda weird, that SUBSTR(expr, 0) = SUBSTR(expr,1). But it works!
Simply use following sql query, which will remove your . from the string.
UPDATE Names SET ID = regexp_replace(id,'\.+','');
Below query will remove all the character, which comes before .
UPDATE Names SET ID = substr(id,instr(id,'.')+1);