How to Drop a Constraint in SQL Server (T-SQL)

In SQL Server, a constraint defines rules that data in a database must comply with. For example, you could have a UNIQUE constraint applied to a column to ensure that any value inserted into that column is unique (i.e. no other row shares the same value).

If later on you need to remove that constraint, here’s how to do it using Transact-SQL.

Continue reading

How to Add a New Column to an Existing Table in SQL Server (T-SQL)

When you create a table in SQL Server using T-SQL, you specify all the columns for that table, along with their data types, any constraints, etc.

But what happens if one day you decide to add a new column to that table? How do you add the new column to the existing table without creating the table again? After all, dropping the table and starting again is usually not an option, as the table will already contain data, and you probably don’t want to have to backup all that data and re-insert it after dropping and creating the table.

The answer is: The ALTER TABLE statement.

Continue reading

How to Add a DEFAULT Constraint to an Existing Column in SQL Server

When using SQL Server, sometimes you need to modify an existing table. For the purposes of this article, say you want to add a DEFAULT constraint to an existing column.

To add a DEFAULT constraint to an existing column, use the ALTER TABLE statement and specify the column and the specific constraint that you want to apply.

Continue reading

CONVERT() vs TRY_CONVERT in SQL Server: What’s the Difference?

You might have noticed that T-SQL includes both a CONVERT() function and a TRY_CONVERT() function that you can use in SQL Server to perform conversions between data types. But if you’re scratching your head over what the difference is between these two functions, read on!

The difference between CONVERT() and TRY_CONVERT() is in the way they handle data types that can’t be converted. One throws an error, while the other returns null. The following examples demonstrate this.

Continue reading

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