How to Change a Column’s Data Type in SQL Server (T-SQL)

When you create a database table, you specify all columns along with their data types. Once created, there’s not normally any intention of changing these data types. After all, whoever designed the schema would have put a lot of thought into what data type each and every column should accept.

However, we all know that things can change. Despite our best efforts at trying to foresee every possible scenario that may hit our database, sometimes that’s not enough.

So what do we do if we need to use Transact-SQL to change the data type of a column in SQL Server?

We use the ALTER TABLE statement to change it of course.

Continue reading

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

AVG() – Calculate the Average Value of a Column in MySQL

When using MySQL, you can use the AVG() function to calculate the average value from a range of values.

For example, you can use this function to find out what the average city population is for a given country or state. Given a country will have many cities, each with different populations, you can find out what the average is between them. One city might have a population of say, 50,000 while another has a population of 500,000. The AVG() function will calculate the average for you.

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

6 Ways to Concatenate a String and a Number in SQL Server

If you’ve ever tried to concatenate a string with a number while using SQL Server, but received an error, this article should clear things up for you. There’s more than one way to perform concatenation using T-SQL in SQL Server, and if you’re concatenating different data types (like a string and a number) then you may receive an error, depending on how you do the concatenation.

The thing to remember when concatenating different data types is that they need to be converted into the same data type first. More specifically, when concatenating a string with a number, the number will need to be converted to a string before it can be concatenated with the string. Fortunately SQL Server/T-SQL makes this a breeze.

This article presents six ways to concatenate strings with numbers using T-SQL.

Continue reading