close
The Wayback Machine - https://web.archive.org/web/20240903121950/https://www.geeksforgeeks.org/sql-encrypt-function/
Open In App

SQL | ENCRYPT Function

Last Updated : 31 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL Encrypt function is used to encrypt a string using UNIX crypt(). The function is based on Unix crypt() system call, hence it returns NULL on Windows systems. The Encrypt function accepts two parameters which are the string and the salt to be encrypted.
The Encrypt function returns a binary string.

Syntax:

ENCRYPT(string, salt)

Parameters Used:

  • string – It is used to specify the plain text string that is to be encrypted using UNIX crypt().
  • salt – It is used to specify a string that is at least 2 characters long and cab be used in the encryption process. If salt is not provided, the ENCRYPT function uses a random value.

Return Value:
The Encrypt function in SQL returns a binary string.

The Encrypt function returns null in the following cases:

  • If salt is less than 2 characters in length, then the Encrypt function returns NULL.
  • If the string is NULL, then the Encrypt function returns NULL.
  • If UNIX crypt() is not available on the system, then the Encrypt function returns NULL.

Supported Versions of MySQL:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1

Example-1: Implementing Encrypt function on a string.

SELECT 
ENCRYPT('xyz'); 

Output:

sf3Le/pz2ApNY 

Example-2: Implementing Encrypt function on a bigger string.

SELECT 
ENCRYPT('geeksforgeeks'); 

Output:

.mblNS3yOZxb2 

Example-3: Implementing Encrypt function on a string by passing both the arguments.

SELECT 
ENCRYPT('geeksforgeeks', '123'); 

Output:

12SrVMQf0pwFU 

Example-4: Implementing Encrypt function on a string by passing less than 2 characters in the salt argument.

SELECT 
ENCRYPT('geeksforgeeks', '2'); 

Output:

NULL 

Since the salt argument is less than 2 characters in length, the Encrypt function returns NULL.

Example-5: Implementing Encrypt function on a NULL string.

SELECT 
ENCRYPT(NULL); 

Output:

NULL 


Similar Reads

Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
QUOTENAME() Function in SQL Server
QUOTENAME() function : This function in SQL Server is used to return a Unicode string with delimiters added in order to make the string a valid SQL Server delimited identifier. Features : This function is used to find a Unicode string with delimiters added. This function accepts only strings and delimiters. This function add delimiters by default i
3 min read
Conversion Function in SQL
When you define expressions and local variables then you should specify what type of data will be stored in those objects such as text data, money, dates, numbers, or characters. Strings Data types such as CHAR and VARCHAR.Decimal values such as FLOAT and REAL.Binary String such as BINARY.Date and Time Data Types such as DATE, TIME, TIMESTAMP, and
4 min read
REVERSE() Function in SQL Server
REVERSE() function : This function in SQL Server is used to reverse the stated string and return the output. Features : This function is used to reverse the stated string. This function accepts strings as parameter. This function always returns string. This function can also take set of integers and reverse it. This function can even reverse float
1 min read
How SYSDATE function works in PL/SQL
The PLSQL SYSDATE function will returns current system date and time on your database. There is no any parameter or argument for the SYSDATE function. The SYSDATE function returns a date value. Note that the SYSDATE function return date and time as "YYYY-MM-DD HH:MM:SS" (string) or as YYYYMMDDHHMMSS (numeric). Syntax: SYSDATE Example-1: SELECT Sysd
2 min read
SESSIONPROPERTY() Function in SQL Server
SESSIONPROPERTY() function : This function in SQL Server is used to return the settings of the session that is specified in the argument section. Features : This function is used to find the settings of the session state.This function comes under Advanced Functions.This function accepts only one parameter namely option. Syntax : SESSIONPROPERTY(opt
2 min read
TRANSLATE() Function in SQL Server
TRANSLATE() function : This function in SQL Server is used to return the translated string of the string stated in the first argument of this function, when the characters stated in the characters argument of the above function are converted into the characters stated in the last argument i.e, translations. Features : This function is used to find
2 min read
RAWTOHEX Function in PL/SQL
The objective is to use the function RAWTOHEX in PL/SQL code. Purpose : This inbuilt function is beneficial for the conversion of a raw value into a character value in its hexadecimal format. Syntax : RAWTOHEX(x) where x - raw value to be converted to hexadecimal value and hexadecimal equivalent in the form of a string value is returned. Here may b
2 min read
SIN() and COS() Function in SQL Server
1. SIN() Function : The SIN() function returns the sine of a number. Syntax : SIN(number) Parameter : Required. A numeric value. number : It is a numeric value. Returns : It returns the float_expression of the sine of a number. Example-1 : When the argument holds a positive number. SELECT SIN(5); Output : -0.95892427466313845 Example-2 : When the a
1 min read
CHAR() Function in SQL
CHAR() : This function could be used to find the character based on the ASCII (American Standard Code for Information Interchange) code. CHAR() function do not support multiple integers as arguments. Syntax : SELECT CHAR(code); Note - Parameter code is mandatory, code evaluates to an integer value from 0 to 255. Example-1: SELECT CHAR(65) AS Result
1 min read
DATEADD() Function in SQL Server
DATEADD() function : This function in SQL Server is used to sum up a time or a date interval to a specified date, then returns the modified date. Features : This function is used to sum up a time or a date interval to a date specified. This function comes under Date Functions. This function accepts three parameters namely interval, number and date.
3 min read
SQL Server LEAD() function Overview
At many instances, user would really like to check the worth of the present row with the worth of the subsequent row. to resolve this problem in SQL Server’s LEAD() window function are often used. LEAD() : Function provides access to a row at a set physical offset following this row. LEAD() function will allows to access data of the following row,
2 min read
ASCII() Function in SQL Server
The ASCII() function returns the ASCII value of the leftmost character of a character expression. Syntax : ASCII(character_expression) Parameter : This method accepts a single-parameter as mentioned above and described below : character_expression : It can be a literal character, an expression of a string, or a column. If more than one character is
2 min read
STR() Function in SQL Server
The STR() function converts a numeric value to a character value. Syntax : STR(float_expression [, length [, decimal]]) Parameter : This method accepts three parameters as mentioned above and described below : float_expression : It is a numeric expression that evaluates to an approximate number with a decimal point. length : It is the total length
1 min read
UNICODE() Function in SQL Server
In this article, we are going to cover UNICODE() function where you will see the Unicode for given specific character or for expression character. UNICODE() : It is the function that gets the integer value for the first character of the input expression, as defined by the Unicode standard. Syntax : UNICODE(ncharacter_expression) Parameter : UNICODE
1 min read
RTRIM() Function in SQL Server
The RTRIM() function removes trailing spaces from a string. Syntax : RTRIM(input_string) Parameter : RTRIM() function accepts single-parameter like input_string. input_string : It is an expression of character or binary data. It can be a literal string, variable, or column. Returns - It returns a string after truncating all trailing blanks. Example
1 min read
Article Tags :