SQL Query to Compare Two Strings
Last Updated :
14 Sep, 2021
SQL stands for Structured Query Language. It is used to communicate with the database. There are some standard SQL commands like 'select', 'delete', 'alter' etc. To compare two strings in SQL Server, there is no direct way. In this article, we will learn how to compare two strings in an MS SQL server and we will provide some examples.
A string function is a function that takes a string value as an input regardless of the data type of the returned value. In SQL Server, there are many built-in string functions that can be used by developers.
We can compare strings by using the IF-ELSE statement.
Syntax:
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
Declare variable:
We can declare variables easily by using the keyword DECLARE before the variable name. By default, the local variable starts with @.
Syntax:
DECLARE @variable_name datatype;
Set values to the variable:
We can assign value to the variables using the SET keyword.
Syntax:
SET @variable_name;
Example 1:
Query:
DECLARE @Name1 VARCHAR(30), @Name2 VARCHAR(20);
Set @Name1='geeks';
Set @Name2='geeks';
If @Name1=@Name2 Select 'match' else Select 'not match';
Output:
The above example shows the string comparison and returns the result as a 'match' because both strings are the same.
Example 2:
Query:
DECLARE @Name1 VARCHAR(30), @Name2 VARCHAR(20);
Set @Name1='geeks';
Set @Name2='geeksforgeeks';
If @Name1=@Name2 Select 'match' else Select 'not match';
Output:
The above example shows the string comparison and returns the result as a 'not match' because both strings are not the same.
Similar Reads
SQL Query to Compare Two Dates In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. Here we will see, SQL Query to compa
2 min read
SQL Query to Match Any Part of String It is used for searching a string or a sub-string to find a certain character or group of characters from a string. We can use the LIKE Operator of SQL to search sub-strings. The LIKE operator is used with the WHERE Clause to search a pattern in a string of columns. The LIKE operator is used in conj
3 min read
How to Compare Two Queries in SQL Queries in SQL :A query will either be an invitation for data results from your info or for action on the info, or each. a question will provide you with a solution to a straightforward question, perform calculations, mix data from totally different tables, add, change, or delete data from info. Cre
2 min read
SQL Query to Compare Results With Today's Date In SQL, comparing results with today's date is a powerful tool for filtering data, managing schedules, including managing tasks, appointments and performing time-sensitive analysis. By using SQL's GETDATE() function we can easily perform this comparison. The ability to filter records based on date c
4 min read
Compare SQL Server Results of Two Queries SQL Server is a versatile database, and it is the most used Relational Database that is used across many software industries. In this article, let us see the comparison of SQL Server Results of Two Queries briefly. By using Azure Data Studio, let us see the concepts by starting with creating the dat
5 min read