In SQL Server, you can use the T-SQL FORMAT()
function to format a time data type. However, if you need to add the AM/PM designator, you’ll need to use a different data type. This is because the time data type is specifically based on a 24 hour clock, and therefore the time is formatted as such.
Remember This When Formatting a TIME Data Type in SQL Server (T-SQL)
In SQL Server, when you use the T-SQL FORMAT()
function to format a time data type, you need to remember to escape any colons or periods in your format string.
This is because the FORMAT()
function relies upon CLR formatting rules, which dictate that colons and periods must be escaped. Therefore, when the format string (second parameter) contains a colon or period, the colon or period must be escaped with backslash when an input value (first parameter) is of the time data type.
4 Ways to Convert a Number to a Percentage in SQL Server (T-SQL)
Here are 4 ways to convert a number to a percentage value in SQL Server.
Strictly speaking, we’re not actually “converting” it to a percentage. We’re formatting the number as a percentage. But in order to do that, we need to convert the number from a numeric data type to a string.
Here are 4 ways to do that.
3 Ways to Convert Decimal to Hexadecimal in SQL Server (T-SQL)
Here are 3 ways to convert from decimal to hexadecimal in SQL Server.
3 Ways to Convert HEX to INT in SQL Server (T-SQL)
Here are 3 ways to convert a hexadecimal value to an integer in SQL Server.
First up are two T-SQL functions that allow you to convert a hex value to an integer. In addition to these functions, there’s also the concept of implicit conversion, which can also produce the same result.
How to Format Numbers as Currency in SQL Server (T-SQL)
In SQL Server, you can use the T-SQL FORMAT()
function to format a number as a currency.
The FORMAT()
function allows you to format numbers, dates, currencies, etc. It accepts three arguments; the number, the format, and an optional “culture” argument. This article specifically deals with using the format argument to specify a currency.
SQL Server String Functions (Full List)
Transact-SQL (T-SQL) includes a number of scalar functions that allow us to perform operation on strings when working with SQL Server. These functions accept an input string, and return either a string or a numeric value.
The following is a list of T-SQL string functions available in SQL Server. Click on each function or operator name to see an explanation of the function, its syntax, and examples.
Continue readingHow the TRANSLATE() Function Works in SQL Server (T-SQL)
In SQL Server, the T-SQL TRANSLATE()
function returns the string provided as a first argument after some characters specified in the second argument are translated into a destination set of characters specified in the third argument.
It’s similar to the REPLACE()
function, but with some important differences.
The TRANSLATE()
function requires 3 arguments; the input string, the characters to be replaced, and the characters to replace them.
How the STRING_ESCAPE() Function Works in SQL Server (T-SQL)
In SQL Server, the T-SQL STRING_ESCAPE()
function escapes special characters in texts and returns the text with escaped characters.
You provide the text and the escape type as arguments when calling the function.
How the STR() Function Works in SQL Server (T-SQL)
In SQL Server, the T-SQL STR()
function returns character data converted from numeric data. The return value has a return type of varchar.
You provide the numeric data as an argument when calling the function. The function then converts it to character data. There are also two optional arguments that you can use to specify the length, and the number of places to the right of the decimal point.