CAST() vs TRY_CAST() in SQL Server: What’s the Difference?

When converting between data types in SQL Server, you will run into various functions that seemingly do the same thing. However, there are usually differences between these functions that might not be apparent at first glance. An example of this is the difference between the CAST() and TRY_CAST() functions.

This article demonstrates the difference between these functions when using SQL Server.

Continue reading

How the SQL Server DIFFERENCE() Function Works

This article aims to help you understand the DIFFERENCE() function, which is a T-SQL function available in SQL Server, Azure, etc.

The key to understanding the DIFFERENCE() function is to understand how Soundex works (or in the context of SQL Server, how the SOUNDEX() function works). This is because the DIFFERENCE() function returns the difference between the Soundex values between two strings.

A Soundex value is four characters long. If two words sound the same, they will share the same four character Soundex value. If they sound similar (but not the same), their Soundex values might share some characters but not all. For example, their Soundex values might have two characters the same and two that are different. If two words sound completely different, none of the characters in their respective Soundex values will be the same.

The DIFFERENCE() function returns a value that ranges from 0 through 4.  This value represents the number of characters in the Soundex values that are the same. 0 indicates weak or no similarity, and 4 indicates strong similarity or the same values.

Continue reading

Left Padding in SQL Server – 3 LPAD() Equivalents

If you use Oracle Database or MySQL, you’re lucky enough to have the LPAD() and RPAD() functions, which allow you to pad a string with a given character (or characters) to its left and/or right.

However, SQL Server (or more precisely, T-SQL), doesn’t include these functions. So if you need some left padding, you’ll need to improvise.

This article presents four options for padding a number with leading zeros in SQL Server. So you can do stuff like turn 7 into 007. Three of these options work on strings, so you can also apply padding to textual data.

Continue reading

How to Return the ASCII Code Value for a given Character in SQL Server

If you ever need to find the ASCII code for a given character when using SQL Server, the T-SQL ASCII() function is probably what you need.

The ASCII() function returns the ASCII code value of the leftmost character of a character expression. Simply provide the character as an argument and SQL Server will return the ASCII value of that character (or the leftmost character in the string).

Continue reading

SQL Server REPLACE() vs TRANSLATE(): What are the Differences?

Starting with SQL Server 2017, you can now use the T-SQL TRANSLATE() function to translate one or more characters into another set of characters.

At first glance, you might think that the TRANSLATE() function does exactly the same thing as the REPLACE() function, but there are significant differences between the two.

Continue reading