In MySQL, we can use the IF EXISTS
clause of the DROP TABLE
statement to check whether the table exists or not before dropping it.
Category: DBMS
Database Management Systems
Fix “Conversion failed when converting the varchar value” When Trying to Concatenate in SQL Server
If you get error Msg 245 that reads “Conversion failed when converting the varchar value…” when trying to perform a concatenation operation in SQL Server, it’s likely that you’re attempting to concatenate a string and a number.
Doing this will result in an error, due to SQL Server trying to add the string and number instead of concatenate them.
To fix this, either convert the number to a string, or use a function like CONCAT()
or CONCAT_WS()
to perform the concatenation.
Format a Number as Currency in SQL
Some DBMSs have functions that allow us to format numbers as currency just by passing the appropriate format string. This converts the number to a string with the applicable currency symbol, group separator, and decimal point (if relevant).
Other DBMSs don’t make it that easy, and you need to do a bit of work first.
Below are examples of using SQL to format numbers as currency in some of the most popular DBMSs.
Continue readingHow to Change the Recovery Model of a SQL Server Database using T-SQL
SQL Server has three recovery models; simple, full, and bulk logged. Each database uses one of these settings.
Backup and restore operations occur within the context of the recovery model of the database
You can change the recovery model of a database by using the ALTER DATABASE
statement along with the SET RECOVERY
option.
Select Everything Before or After a Certain Character in MariaDB
In MariaDB, you can use the SUBSTRING_INDEX()
function to return everything before or after a certain character (or characters) in a string.
Get the Size of All Databases in PostgreSQL (psql)
When using psql with PostgreSQL, we can use the \list+
command to return information about all databases on the server.
We can also use the short form (\l+
) to achieve the same result.
Concatenate a String and a Number in SQL
In most cases, concatenating a string and a number in SQL is no different to concatenating two strings.
Most DBMSs will concatenate the string and number as if they were both strings. This is because the number is usually converted to a string before the concatenation operation.
Continue readingHow to List All Views in Oracle Database
You can use the following views to return a list of views in Oracle Database.
Continue readingHow to Change the Table Border Style in the psql Query Results
When using psql
to run queries against PostgreSQL, we can use the \pset border
and \pset linestyle
commands to change the border of the table that displays our results.
Oracle GROUP_CONCAT() Equivalent
Some RDBMSs have a GROUP_CONCAT()
function that allows you to return a query column as a delimited list (for example, a comma separated list). MySQL and MariaDB are two that have such a function.
PostgreSQL and SQL Server have similar functions called STRING_AGG()
.
Oracle, on the other hand, has the LISTAGG()
function that does pretty much the same thing (and perhaps more).
So you could say that LISTAGG()
is Oracle’s GROUP_CONCAT()
equivalent.