I am trying to run an update query which will strip - from a field. For example:
ID NAME
------------
1 you-me
On updating, I want to strip the "-". The result should be:
ID NAME
------------
1 youme
Is there any sql command to do this? I am using DB2.
A straight REPLACE function then?
update tbl set col = REPLACE(col, '-', '')
You can just use SQL REPLACE function
UPDATE table
SET column = REPLACE(column, '-', '');
See this documentation
Related
ID column has some values starting with ' and some do not. How can I use function to delete all ' in ID Column.
Thanks a lot!
You should provide your attempts next time, that shows you're actually trying to achieve something on your own but here you go:
update <your_table> t
-- desired columns go here
set t.id = regexp_replace(t.id, '^''{1}', '')
-- can be omitted if you want to scan all records
where t.id like '''%';
If you want to replace all quote characters you can do this:
update <your_table> t
-- desired columns go here
set t.id = replace(t.id, '''', '')
where t.id like '''%';
I have the following value format :
1234567890
I want to convert it to the following format : 123-45A67890
Table Name: Test
Column Name : MyCode
Note that I am using Microsoft SQL 2012.
You can use STUFF . Something like this
DECLARE #v VARCHAR(15) = '1234567890'
SELECT STUFF(STUFF(#v,4,0,'-'),7,0,'A')
Your SELECT would be
SELECT STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
FROM Test
Your UPDATE would be
UPDATE Test
SET MyCode = STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
Does SQL Server support the PASTE function?
paste(paste('1234567890',7,0,'A'),4,0,'-')
For example:
select paste(paste(column_name,7,0,'A'),4,0,'-') from table_name
or
update table_name set column_name = paste(paste(column_name,7,0,'A'),4,0,'-')
Here what tried with a variable #a:
declare #a varchar(100)='1234567890'
select STUFF(STUFF(#a,4,0,'-'),7,0,'A') --gives--> '123-45A67890'
Likely you can use it to update your table, which
Adds Hyphen (-) after first 3rd character,
Adds letter 'A' after 2 letters just after hyphen...
update Test set MyCode = STUFF(STUFF(MyCode,7,0,'A'),4,0,'-')
More about STUFF() function in SQL
I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql
How do I achieve the following by a stored procedure or sql script in sql server?
Say for example, I have a-b in one of the columns and I need to update that column to a - b instead.
How do I insert spaces before and after the dash in existing data in my database.
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, '-', ' - ')
update TableName set ColumnName = Replace(ColumnName, '-', ' - ') where SomeCondition ...
I am looking to replace values in a particular column. For example the following column values
column name
----------
Test1
Test2
Test3
Test12
should be (replacing est1 with rest1)
column name
----------
Trest1
Test2
Test3
Trest12
Use REPLACE:
SELECT REPLACE(t.column, 'est1', 'rest1')
FROM MY_TABLE t
If you want to update the values in the table, use:
UPDATE MY_TABLE t
SET column = REPLACE(t.column, 'est1', 'rest1')
If you need to update the value in a particular table:
UPDATE TABLE-NAME SET COLUMN-NAME = REPLACE(TABLE-NAME.COLUMN-NAME, 'STRING-TO-REPLACE', 'REPLACEMENT-STRING');
where
TABLE-NAME - The name of the table being updated
COLUMN-NAME - The name of the column being updated
STRING-TO-REPLACE - The value to replace
REPLACEMENT-STRING - The replacement
In Oracle, there is the concept of schema name, so try using this
update schemname.tablename t
set t.columnname = replace(t.columnname, t.oldvalue, t.newvalue);
I'm using Version 4.0.2.15 with Build 15.21
For me I needed this:
UPDATE table_name SET column_name = REPLACE(column_name,"search str","replace str");
Putting t.column_name in the first argument of replace did not work.