Useful SQL Queries
How to concatenate values from previous row to current row in SQL?
Answer:
How to check the database recovery progress in SQL Server?
How to identify similarly pronounced words in SQL server?
There are two functions in SQL Server that are used to identify whether the two strings are pronounced similarly or not.
They are
-
SOUNDEX()
- This function takes a string as parameter and returns a four-character code. This code is called as Soundex. When this code is calculated it basically ignores the vowels (A, E, I, O, U), H, W, and Y unless they are the first letter of the string. -
DIFFERENCE()
- This function takes two strings as parameter and returns a integer value from 1 to 4. This function internally calculates the SOUNDEX code for each of the string and find the difference between the twoSOUNDEX
code.
The output of the DIFFERENCE
function
Output | Meaning |
---|---|
1 | Not similar |
2 | Very less similar |
3 | Some what similar |
4 | Exact match/ Mostly similar |
If you like this question, you may also like these…
- What happens to the records inserted into a table variable, if the transaction rollback?
- How to sort result set in custom order in SQL?
- How to concatenate values from previous row to current row in SQL?
How to concatenate multiple rows into a single string in SQL?
Write a SQL query to concatenate multiple rows into a single string?
Solution
This can be implemented in different ways.
-
STUFF function with XML path
We are concatenating comma with the product category name and then converting the products as single string using xml path. Then replace the leading comma with blank using stuff
function.
-
Substring function with XML path
This is similar to the previous appraoch. But instead of replacing the leading comma, we are you using substring
function to select all the text except the leading comma. We have given approximate length as 10000 and this can be modified based on the scenario.
-
STRING_AGG function
This string_agg
function was introduced in SQL Server 2017. It concatenates the values of string and places separator(comma in our case) values between them. The separator isn’t added at the end of string. If there are NULL, then it will be ignored and separator will not be added for that.
-
COALESCE function
This is an old way of concatenating the rows into a string. If there is a NULL, the output will NULL as well. We should either filter out the NULL or replace a NULL with other string value. In the solution below we have replaced with a blank value and this is the reason there is two commas in the result.
Output:
How to get any object’s definition in SQL Server?
You can use the below query to get the definition of any object in SQL Server. You can replace the object type in the query to get the definition of the object.
Sample Output
How to find a SQL Server agent jobs last executed and next scheduled date time?
You can execute the below query to get the list of SQL Server agents jobs and when it was last executed and time it took to complete the job and when it is scheduled to run next.
Output:
List of all foreign keys and referenced tables and columns
Sample Output
List out all the indexes defined on a table and column details.
Result:
How to replace the first instance of the string in T-SQL?
You can use the combination of stuff
, charindex
and len
function to replace the first occurrence of the string.
How to get all the computed columns in a database in SQL Server?
How to search for a table or column in all the databases in a server
To search for a table across databases in a server
Sample output:
To search for a column in tables across databases in a server
How to check database restore history and backup file used for restore in SQL Server?
How to split a string into a list of values in SQL?
I have created a function to split a string based on the delimiter value and return the result as a single column of values in a table.
Sample output:
How to search for a list of words contains in a string column?
To search whether the list of words contains in a column, you can implement it using a series of conditions with LIKE
and OR
operators. But to search for additional word, you need to add another condition.
Instead of the above approach, you can create the below function dbo.fn_contains
that search for the words in a string column. This function uses another function dbo.fn_string_to_list
which converts a string to a list of values.
Sample query to search for a list of words in a string column.