Trying to replace a character of a column within all the Records of a table,
Wanted to change '_' with ' '
Any snippets how doing that?
Notes :
This is the primary key of type Nvarchar
Database is SQL Server CE
using management studio
You can use REPLACE to do that :
Update dbo.table
Set col = replace(col, '_', '');
Where col is the name of the column that you need to manipulate ...
Related
I have a table called Manufacturers which contains a column Name with a data type of Varchar(100) that has 5761 entries such as XYZ(XYZ Corp).
I wish to insert a space between the Z and the (.
I have seen the STUFF and LEFT commands but can't seem to figure out if they apply to my scenario.
You can try the REPLACE (Transact-SQL)
function as shown below.
update <yourTableName>
set <yourColumName> = replace(<yourColumName>, '(', ' ( ')
where <put the conditions here>
Here is an implementation to you.
create table test (Name varchar(20))
insert into test values ('XYZ(XYZ Corp)')
--selecting before update
select * from test
--updating the record
update test
set Name = replace(Name, '(', ' (')
where name like '%(%' --Here you can add the conditions as you want to restrict the number of rows to be updated based on the available data or the patterns.
--selecting after update
select * from test
Live Demo
I would recommend:
update Manufacturers
set name = replace(name, '(', ' (')
where name like '%[^ ](%';
This will only update rows where there is not already a space before the open paren.
Note: If you have multiple open parens, it will update all of them. If that is an issue, ask a new question with appropriate sample data.
I need a prepare an SQL script to be given to my production support team to run this in production. We have created a new column in the DB2 table. This column should be filled with the data by concatenating 3 column values of the same table in the same row.
To give a history, all the reason text which are entered in the front end are inserted into the request table by unstringing into 3 columns. Since they had limited length, we created a new column with increased length and going forward all insert will go into the new column. But we need to move all the existing data in the old 3 columns to his new one. S this SQL update is just an one time exercise.
Table: tab_request
We added new column to have a increased character length and to align with other table nomenclature. Now I need the script to update the column reasontext as below
update set field1 || .... should be your dml script. use coalesce() function to convert those null values to ''.
Update table1
set reasontext =
(coalesce(reason_1, '') || ' ' || coalesce(reason_2,'') || ' ' || coalesce(reason_3,''))
update tab_request set reasontext=CONCAT(reason_1,' ',reason_2,' ',reason_3)
If you want to avoid unnecessary spaces -- in the event that some reasons are NULL -- then you can use trim() with coalesce():
update table1
set reasontext = trim(coalesce(' ' || reason_1, '') ||
coalesce(' ' || reason_2, '') ||
coalesce(' ' || reason_3, '')
);
This is equivalent to concat_ws() available in some databases.
In SQL Server 2012 when searching for a where a column name = 'C CANTU ' it is returning rows where the column in question = 'C CANTU'.
This is with the collation SQL_Latin1_General_CP850_BIN2.
In SQL Server 2005 these two values are not equal, and these rows are not returned.
Is there a setting in SQL Server 2012 for this? They are nvarchar columns. Same scripts ran against each. We are just working on trying to upgrade and found inconsistencies in the final result because of this.
Basically what is happening is that the column is being adjusted with additional trailing spaces to make the lengths match. A couple of workarounds:
DECLARE #p NVARCHAR(255) = N'C CANTU ';
SELECT col FROM dbo.table
WHERE col LIKE LEFT(#p,1) + '%' AND col + 'x' = #p + 'x'; -- will seek
SELECT col FROM dbo.table
WHERE col = #p
AND DATALENGTH(col) = DATALENGTH(#p); -- will seek (and add a filter)
Note that LEN() will ignore trailing spaces, but DATALENGTH() will not.
As I mentioned in the comments, I strongly recommend correcting any queries you come across where nvarchar columns are compared to string literals without using the N prefix.
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 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