copy part of string from one column into another - sql

I've got a column with some string data that somewhere has 'T##' (## being a two digit number) I want to copy this into another column, how do I do that?
something like this:
abc-T03-def -> 03

For Microsoft SQL Server:
update YourTable
set NewColumn = substring(OldColumn, patindex('%T[0-9][0-9]%', OldColumn) + 1, 2)
where patindex('%T[0-9][0-9]%', OldColumn) <> 0

Related

Update SQL Server table with multiple values using set

How to update multiple values in a SQL server table? Here are the values in the table for the column X:
80, 81, 90
I want these to be updated to 0080, 0081, 0090
I have been updating each value this way:
update TBL_NAME
set TXN_ID = '0094'
where TXN_ID = '94'
I know the 'IN' operator didn't work. Any help would be appreciated. Thank you in advance.
This only makes sense if X is a string. In that case, you can use:
update tbl_name
set txn_id = right('0000' + txn_id, 4)
where txn_id <> right('0000' + txn_id, 4);
If x is a number of any sort, you cannot prepend it with zeros. You would have two options:
Add a generated column that is a string padded with zeros.
Convert the column to a string and then pad the value.

Delete fixed string contained in SQL Server Table Column

I have a table with filed 'myData' of type nvarchar containing sometimes a data ending with the string '|||' that I want to remove.
Obviously the data is not fixed, so I can't just use
UPDATE myTable
SET myData = REPLACE(myData, 'oldString', 'newString')
as this would work just for one record (e.g. oldString = '12-feb-17|||' and newString = '12-feb-17')
How can I do it globally?
You can do:
UPDATE myTable
SET myData = LEFT(myData, LEN(myData) - 3)
WHERE myDATE LIKE '%|||';

Update all records to remove first 3 letters of string SQL MS Access 2013?

There is a set of data that I pulled from a server and in one of the fields, every value has the first 3 letters "DFT" with 2 or 3 letters following that. I want to use an SQL statement along the lines of:
UPDATE table_name
SET column1=[string after first 3 letters]
WHERE [first 3 letters of string]='DFT';
How would I go about doing this?
UPDATE table_name
SET column1 = MID(column1, 4, LEN(column1) - 3)
WHERE MID(column1, 1, 3) = 'DFT'

UPDATE multiple values in table t-sql

I have multiple columns where one of them has varchar values as follow.
1
2
4
02
05
6
03
06
123
32
87
I want to find any record that has values starting with 0 > remove that 0 (so 02 > 2) and update that field so there is no value starting with 0.
How can I go about doing that instead of manually updating each record that has 0 in the front? Is that even possible?
Thank you
The following code removes the leading zeros by casting the value to an integer and back to a character string:
update t
set col = cast(cast(col as int) as varchar(255))
where t.col like '0%'
You can use the following:
update yourtable
set col = substring(col, 2, len(col)-1)
where left(col, 1) = '0'
See SQL Fiddle with Demo
Run this query until everything is filtered out I guess...
UPDATE Table SET Column=RIGHT(Column, LEN(Column)-1) WHERE Column LIKE '0%';
[Edit] Gordon's approach is probably better.

sql update part string and trim trailing value

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