SQL Query to Compare Two Strings Last Updated : 14 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Comment More infoAdvertise with us Next Article SQL Query to Compare Two Dates R romy421kumari Follow Improve Article Tags : SQL Blogathon Blogathon-2021 SQL-Query 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 SQL Query to Get Only Numbers From a String As we know in an SQL database we can insert any type of data. Sometimes in the productions server, the data gets corrupted by two or more rows being merged and being saved in a column. In that case, we can extract the numeric part from that string and save it again. So in this article, we will learn 2 min read How to Compare Time in MS SQL Server? To compare time in MS SQL Server, use the comparison operators (=,<,>, etc.). In this article, we will be making use of the Microsoft SQL Server as our database. and comparing times using both pre-defined dates and the current date and time with the GETDATE() function.First, let's create a dat 3 min read Like