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

How to Convert a String to Hexadecimal in MySQL – HEX()

In MySQL, you can convert a string to its hexadecimal representation by using the HEX() function.

The HEX() function works on both strings and numbers, however, its handling of each of these is slightly different. When used to convert a number, it returns a hexadecimal string representation of the number treated as a longlong (BIGINT) number. But when converting a string, it returns a hexadecimal string representation of the string where each byte of each character is converted to two hexadecimal digits.

Continue reading

3 Ways to “Unhex” a String in MySQL

MySQL includes various functions that can assist you when working with hexadecimal values. One of the most obvious functions is the HEX() function, which enables you to convert a string into its hexadecimal equivalent.

However, there may be times where you want to convert a hexadecimal string back to a more readable string. That’s what this article is about.

Here are three ways to “unhex” a string in MySQL:

  • The UNHEX() function
  • The X hexadecimal notation
  • The 0x notation

Below are examples of each of these methods.

Continue reading

How to “Unhex” a Number in MySQL

In MySQL, you can unhex a string using the UNHEX() function. But you can’t unhex a number with that function.

To unhex a number in MySQL, use the CONV() function instead.

The CONV() function allows you to convert numeric values between different numbering systems. For example, you can convert between say decimal and binary, octal to decimal, or, more relevant to this article, between hexadecimal and decimal.

Continue reading

How to Use STRCMP() to Compare 2 Strings in MySQL

One of the string functions in MySQL is STRCMP().  This function allows you to compare two strings according to their sort order.

The function accepts two arguments. Each argument is the string to compare. It returns either 1, -1, or 0, depending on whether the first string is larger, smaller, or the same size as the second string, according to sort order.

Continue reading

How to Pad a String with Leading/Trailing Characters in MySQL – LPAD(), RPAD()

Sometimes you need to pad a string with spaces. Or perhaps you’ll pad it with another character. Sometimes you need to pad it on the left. Other times you need to pad it on the right. Or maybe you need to pad it on both sides.

All of the above can be done in MySQL using the LPAD() and/or RPAD() string functions.

Continue reading