w3resource logo


>Sql translate

SQL TRANSLATE() function

rating Average rating 8 out of 10. Total 15 users rated.

<<PreviousNext>>

Description

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 :

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 :

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

Sql TRANSLATE() nemeric

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 :

SELECT TRANSLATE(agent_name,'abcdefghijk',
'@#$%^&*()}]' )
FROM agents;

Output

Sql TRANSLATE() character

Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

See our Model Database

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.



<<PreviousNext>>

Rate this tutorial


Your Rating: not set

Share this tutorial

RSS Feed