SQL TRANSLATE() function
TRANSLATE() function
The SQL TRANSLATE() function replaces a sequence of characters in a string with another sequence of characters. The function replaces a single character at a time.
Syntax:
TRANSLATE(char_value USING translation_name)
Parameters:
Name | Description |
---|---|
char_value | A string. |
translation_name | A character set. |
DB2 Syntax:
TRANSLATE(string-expression,to-string,from-string,pad)
PostgreSQL Syntax:
TRANSLATE(string text, from text, to text)
Oracle Syntax:
TRANSLATE(char_value USING {CHAR_CS | NCHAR_CS})
Example:
To get the string where all occurrences of 'abcdefghijklmnopqrstuvwxyz' will be replaced with corresponding characters in the string 'defghijklmnopqrstuvwxyzabc', the following SQL statement can be used:
SQL Code:
SELECT TRANSLATE ( 'this is my string',
'abcdefghijklmnopqrstuvwxyz',
'defghijklmnopqrstuvwxyzabc' )
encode_string
FROM dual ;
Here, "encode_string" is an alias which will come as a column heading to the output.
SQL TRANSLATE() function with numeric values
To get a new string from the string +91 25-2469782464 where all occurrences of '0123456789-+' will be replaced with corresponding characters in the string '6789012345+-' , the following SQL statement can be used:
SQL Code:
SELECT TRANSLATE( '+91 25-2469782464',
'0123456789-+','6789012345+-' )
encode_number
FROM dual;
Here, "encode_number" is an alias which will come as a column heading to the output.
Output:
ENCODE_NUMBER ----------------- -57 81+8025348020
SQL TRANSLATE() function with character values
Sample table: agents
To get a new string from agent_name where all occurrences of 'abcdefghijk' will be replaced with corresponding characters in the string '@#$%^&*()}]' from the agent table, the following SQL statement can be used:
SQL Code:
SELECT TRANSLATE(agent_name,'abcdefghijk',
'@#$%^&*()}]' )
FROM agents;
Output:
TRANSLATE(AGENT_NAME,'ABCDEFGHIJK','@#$%^&*()}]') -------------------------------------------------- Al^x Su##@[email protected] B^n}@m)n [email protected]@sun%@r Al&or% [email protected]) [email protected] [email protected]@][email protected] Lu$)%@ An%^rson Mu]^s( M$D^n [email protected]
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework