SQL query to replace commas by pipe symbol - sql

How to write an SQL query to replace commas by pipe symbol in a string, for example: abc, def.

use following query
update DATABASE_NAME.TABLE_NAME
set FIELD_NAME = replace(
FIELD_NAME,
‘find this string’,
‘replace found string with this string’
);
also u can use for select only
SELECT REPLACE(‘www.mysql.com’, ‘w’, ‘Ww’);

There is no such command in the SQL standard, but most vendors implement this function as "replace():
SQL Server: replace()
http://msdn.microsoft.com/en-us/library/ms181984.aspx
Oracle: replace()
http://www.oradev.com/oracle_string_functions.jsp
DB2: replace ()
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_scalarfunctionsintro.htm
mySQL: replace ()
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Here are some SQL Server examples:
http://www.sqlteam.com/article/using-replace-in-an-update-statement
SELECT Replace('SQLTeam.com Rocks!', 'Rocks', 'is cool') -- returns literal
Update dbo.authors
Set city = replace(city, 'Salt', 'Olympic'); -- Updates table

Declare #str varchar(100)='abc,def'
SELECT REPLACE(#str,',','|')

Related

Replacing hyphen and merging the variable in sql

I have 'zipcode' column with value 19707-1234. I have to remove hyphen in between and change the value to 197071234. Can anyone please help me, how to do this using sql query irrespective of index?
You can simply use the replace function:
SELECT REPLACE(zipcode, '-', '')
We a have built in replace function,you can use that
Declare #zipcode varchar(50)='19707-1234'
Select replace(#zipcode,'-','')
Use inbuilt REPLACE function of SQL.
SELECT REPLACE('19707-1234','-','');
In mysql I have used below query for replacing hyphen
*SELECT concat( SUBSTRING_INDEX('0125-3256','-',1),SUBSTRING_INDEX('0125-3256','-',-1)) as replace_hypen*
Output of this query = '01253256'
So you can use for zipcode code column
SELECT concat( SUBSTRING_INDEX(zipcode,'-',1),SUBSTRING_INDEX(zipcode,'-',-1)) as replace_hypen

Replace ; with blank in SQL

I have a table like this:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT #T
SELECT 'Amplifier'
UNION ALL SELECT ';'
UNION ALL SELECT 'Regulator'
How can I replace the semicolon (';') with blank ('').
Expected Output:
Amplifier
'' -- here semicolon replace with blank
Regulator
If you want to replace ALL semicolons from any outputted cell you can use REPLACE like this:
SELECT REPLACE(note,';','') AS [note] FROM #T
Fetching from the given table, use a CASE statement:
SELECT CASE WHEN note = ';' THEN '' ELSE note END AS note FROM #T;
replace() would replace all occurrences of the character. Doesn't seem like you'd want that. This expression only replaces exact matches of the whole string.
It looks like you need to REPLACE all your semicolons:
DECLARE #T TABLE
(note VARCHAR (50))
INSERT INTO #T
SELECT REPLACE(SourceColumn, ';', '')
FROM SourceTable
SQL Server 2017 introduced the function TRANSLATE where you can specifiy a list of characters to be replaced
SELECT TRANSLATE('MAX(0,MIN(h-36,8))', '(,-)', ' ') -->'MAX 0 MIN h 36 8 '

Setting a character variable which consists of this (') character

I want to set a variable to expressions like this 'word' in SQL which consists of this (') character.
How can I do that?
For SQL Server to show ' use '' into select
SELECT 'aa''a'
You can use variable with Single quotes as following :-
DECLARE #varName VARCHAR(50)
SET #varName = '''word'''
SELECT #varName as colName
Output :- 'word'
Sample Fiddle

updating a text with additional characters in SQL

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

Replacing / with - in SQL Server using Select Command

How can I replace one char from a SQL Server Select command without changing the value in the table itself?
Example:
Select Col_Name
from TABLE
returns
**RANDOM/ANSWER**
How do I modify the Select query to get **RANDOM-ANSWER** instead? So the / is replaced by -. But the data in the table remains unchanged
The replace function should do the trick:
SELECT REPLACE(my_column, '/', '-')
FROM my_table
Select Col_Name, REPLACE(Col_Name, '/', '-') AS New_Column
from TABLE
To replace all the sepecific character/string found in the data with the new character/string.
DECLARE #myValue VARCHAR(50) = 'RANDOM/ANSWER';
SELECT REPLACE(#myValue, '/', '-');
-- o/p:
-- RANDOM-ANSWER