Here are three examples that use SQL to find and select the row with the maximum value in a given column.
The examples work in most major RDBMSs, including MySQL, MariaDB, PostgreSQL, SQLite, Oracle, and SQL Server.
Continue readingHere are three examples that use SQL to find and select the row with the maximum value in a given column.
The examples work in most major RDBMSs, including MySQL, MariaDB, PostgreSQL, SQLite, Oracle, and SQL Server.
Continue readingDropping 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 readingMost of the major RDBMSs have a function that enables us to return our query results as a comma separated list.
That is, we can use such a function to convert each row into a separate list item, within a comma separated list.
Below are examples of how to achieve this in some of the more popular RDBMSs.
Continue readingHere are three examples of returning rows that contain alphanumeric characters in SQL Server.
Alphanumeric characters are alphabetic and numeric characters.
Continue readingThe following example uses T-SQL to delete duplicate rows in SQL Server.
To be more specific, it deletes duplicate rows but keeps one. So if you have two identical rows, it deletes one of them and keeps the other. In other words, it de-dupes the table.
Continue readingIf 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 readingIn 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.
If you need to find rows that contain lowercase letters in SQL Server, you could try one of the following options.
Continue readingIf 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 readingT-SQL doesn’t include the IF NOT EXISTS
clause with its CREATE TABLE
statement, like some other DBMSs do.
Therefore, if we want to check for the existence of the table before we create it in SQL Server, we need to use other methods.
Continue reading