4 Ways to Check if a Table Exists Before Dropping it in SQL Server (T-SQL)

Dropping a table in SQL easy. You simply use DROP TABLE myTable where myTable is the name of the table you want to drop. You can use this method to drop a table in SQL Server via T-SQL script.

But you’ll get an error if the table doesn’t actually exist. That is, unless you check for the existence of the table first.

Below are four ways of using T-SQL to check whether the table exists before dropping it.

Continue reading

Fix Error Msg 4151 “The type of the first argument to NULLIF cannot be the NULL constant because the type of the first argument has to be known” in SQL Server

If you get error Msg 4151 “The type of the first argument to NULLIF cannot be the NULL constant because the type of the first argument has to be known” in SQL Server, it’s because you’re passing a null value as the first argument to the NULLIF() function.

To fix this error, make sure you do not pass the null constant as the first argument to the function. Or if you do, then convert it to a specific data type.

Continue reading

How to Get the Recovery Model of a Database in SQL Server using T-SQL

In SQL Server, a recovery model is a database property that controls how transactions are logged, whether the transaction log requires (and allows) backing up, and what kinds of restore operations are available. Databases can use one of the following three recovery models: simple, full, and bulk-logged.

You can query the sys.databases catalog view to get a list of databases and their recovery models.

Continue reading

Fix Msg 241 “Conversion failed when converting date and/or time from character string” in SQL Server

If you’re getting SQL Server error Msg 241 that reads Conversion failed when converting date and/or time from character string, it’s probably because you’re trying to convert a string to a date/time value, but that particular string can’t be converted to the date/time type specified.

Continue reading