This article demonstrates how to drop a column in a SQL Server database, when that column has an existing constraint (Hint: You need to drop the constraint first).
how to
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.
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.
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.
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
Xhexadecimal notation - The
0xnotation
Below are examples of each of these methods.
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.
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.
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.
How to Add Leading Zeros to a Number in MySQL
When working with MySQL, you might occasionally encounter situations where you need to pad a bunch of numbers with leading zeros.
Perhaps you have a requirement that all numbers have three digits, but in the data you’ve been supplied, the numbers range from a single digit, to two, and maybe three digits. Your requirement might be to pad all numbers with leading zeros, but only to make up for any shortfall in the three digit requirement.
The LPAD() function does exactly what you want in this scenario.
How to Replace all Occurrences of a String with another String in SQL Server – REPLACE()
In SQL Server, you can use the T-SQL REPLACE() function to replace all instances of a given string with another string. For example, you can replace all occurrences of a certain word with another word.