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).
Tag: mssql
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.
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.
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.
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.
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.
How the SQL Server SOUNDEX() Function Works
Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. It was developed and patented in 1918 and 1922.
One of the functions available in SQL Server is the SOUNDEX()
function, which returns the Soundex code for a given string.
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.
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.
How to Return the Unicode Value for a given Character in SQL Server – UNICODE()
One of the functions included in T-SQL is the UNICODE()
function. You can use this function with SQL Server (and Azure) to return the Unicode value of a given character.
This function works similar to the ASCII()
function, except that it returns the Unicode value.