Replacing hyphen and merging the variable in sql - 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

Related

SQL wont let me replace a quote in SELECT statement

I am trying to remove double quotes " from a column in my SQL export and I get an error, after researching the proper way... this is one of the ways I have tried....
SELECT
'293453' as custnum,
REPLACE(Orders.Order_Comments, '"', '') as FULFILL1,
OrderDetails.OrderID as OrderID2, etc.
The resulting error is:
Your SQL is invalid: Argument data type text is invalid for argument 1 of replace function.
Your Orders.Order_Comments columns is of type text. You can't use the REPLACE() function with that data type. It only works with char/varchar/nchar/nvarchar.
To fix this, the best thing to do is ALTER the table to use a varchar(max) column. The text type is depcrecated, anyway. But, if that's not an option, you'll have to cast it in the query:
REPLACE(CAST(Orders.Order_Comments as varchar(max)), '"', '')
Note that this is potentially very slow.
Take a look at this answer: SQL Server find and replace in TEXT field
The text data type is not valid for use with replace, so you need to cast it as VARCHAR(MAX)
REPLACE(CAST(Orders.Order_Comments AS VARCHAR(MAX)), '"', '')
Try this:
select REPLACE(isnull(Orders.Order_Comments,''), '"', '') as order_comments
from orders

RTrim not working in sql server 2005-2008

Looked over the net but could find a solution
Could anybody tell me why the following would not work?Am I missing the obvious!!
SELECT RTRIM('99999 ')
You cannot see very well from above after 99999 I have many empty spaces.
My column contains numbers and I could do
SELECT LEFT(Myfield, 8)
and this would work but I don't know the length of the column.
Any suggestions?
Thanks
For Rtrim you can use
Declare #temp varchar(100)
Set #temp = '99999 '
Select Rtrim(#temp)
Output:= 99999

SQL query to replace commas by pipe symbol

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,',','|')

How do I remove single quotes from a table in postgresql?

I searched around quite a bit, it would be great if someone could link me to a solution or answer my query.
The thing is I have a postgresql table that contains a lot of single quotes and I cant figure out how to get rid of them, because obviously this
update tablename set fieldname= NULL where fieldname=' ;
wont work.
Better use replace() for this:
UPDATE tbl SET col = replace(col, '''', '');
Much faster than regexp_replace() and it replaces "globally" - all occurrences of the search string. The previously accepted answer by #beny23 was wrong in this respect. It replaced first occurrences only, would have to be:
UPDATE tbl SET col = regexp_replace(col, '''', '', 'g');
Note the additional parameter 'g' for "globally". Read about string functions in the manual.
Aside: the canonical (and SQL standard) way to escape single quotes (') in string literals is to double them (''). Using Posix style escape sequences works, too, of course. Details:
Insert text with single quotes in PostgreSQL
update tablename set fieldname= NULL where fieldname='''' ;
or
update tablename set fieldname= NULL where fieldname=E'\'' ;
insert into table1(data) values ($$it's a string, it's got some single quotes$$)
Use $$ before and after the string. It will insert data.

select * from table_name where column like '&nbsp'

I need to find records containing html code such as '&nbsp' But when I try to run the select * from table_name where column like '&nbsp%'
I got prompt asking for the value of nbsp. I guess the database thinks that nbsp is a parameter. I am wondering if the is an escape character so that I can tell the database that "&" is part of my query string. I tryed '\&nbsp' but didn't work.
My environment is Oracle 9i with sqlplus client.
Thanks.
Have a look at this:
SQL Plus FAQ
e.g.
SET ESCAPE '\'
SELECT '\&abc' FROM dual;
Easier way:
SET DEFINE OFF
See:
SET DEFINE
The backslash should work, but I think you need to start your query with
SET ESCAPE ON
In PL/SQL, you would use:
BEGIN select <Column> from <Table_name> into <Variable> where <Column> LIKE '\&nbsp\%' ESCAPE '\'; END
/
Resources:
Wilcards in SQL on PSOUG.org
LIKE Condition in Oracle® Database SQL Language Reference 11g Release 2 (11.2)