{"id":165,"date":"2018-12-06T10:19:33","date_gmt":"2018-12-06T10:19:33","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/06\/sql-server-functions\/"},"modified":"2024-03-03T18:01:13","modified_gmt":"2024-03-03T18:01:13","slug":"sql-server-functions","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-server-functions\/","title":{"rendered":"SQL Server Functions"},"content":{"rendered":"<p class=\"intro\">SQL Server has many built-in functions. SQL Server Functions<\/p>\n<p class=\"intro\">This reference contains string, numeric, date, conversion, and some advanced functions in SQL Server.<\/p>\n<h2>SQL Server String Functions<\/h2>\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th><strong>Function<\/strong><\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><strong>ASCII<\/strong><\/td>\n<td>Returns the ASCII value for the specific character<\/td>\n<\/tr>\n<tr>\n<td><strong>CHAR<\/strong><\/td>\n<td>Returns the character based on the ASCII code<\/td>\n<\/tr>\n<tr>\n<td><strong>CHARINDEX<\/strong><\/td>\n<td>Returns the position of a substring in a string<\/td>\n<\/tr>\n<tr>\n<td><strong>CONCAT<\/strong><\/td>\n<td>Adds two or more strings together<\/td>\n<\/tr>\n<tr>\n<td><strong>Concat with +<\/strong><\/td>\n<td>Adds two or more strings together<\/td>\n<\/tr>\n<tr>\n<td><strong>CONCAT_WS<\/strong><\/td>\n<td>Adds two or more strings together with a separator<\/td>\n<\/tr>\n<tr>\n<td><strong>DATALENGTH<\/strong><\/td>\n<td>Returns the number of bytes used to represent an expression<\/td>\n<\/tr>\n<tr>\n<td><strong>DIFFERENCE<\/strong><\/td>\n<td>Compares two SOUNDEX values, and returns an integer value<\/td>\n<\/tr>\n<tr>\n<td><strong>FORMAT<\/strong><\/td>\n<td>Formats a value with the specified format<\/td>\n<\/tr>\n<tr>\n<td><strong>LEFT<\/strong><\/td>\n<td>Extracts a number of characters from a string (starting from left)<\/td>\n<\/tr>\n<tr>\n<td><strong>LEN<\/strong><\/td>\n<td>Returns the length of a string<\/td>\n<\/tr>\n<tr>\n<td><strong>LOWER<\/strong><\/td>\n<td>Converts a string to lower-case<\/td>\n<\/tr>\n<tr>\n<td><strong>LTRIM<\/strong><\/td>\n<td>Removes leading spaces from a string<\/td>\n<\/tr>\n<tr>\n<td><strong>NCHAR<\/strong><\/td>\n<td>Returns the Unicode character based on the number code<\/td>\n<\/tr>\n<tr>\n<td><strong>PATINDEX<\/strong><\/td>\n<td>Returns the position of a pattern in a string<\/td>\n<\/tr>\n<tr>\n<td><strong>QUOTENAME<\/strong><\/td>\n<td>Returns a Unicode string with delimiters added to make the string a valid SQL Server delimited identifier<\/td>\n<\/tr>\n<tr>\n<td><strong>REPLACE<\/strong><\/td>\n<td>Replaces all occurrences of a substring within a string, with a new substring<\/td>\n<\/tr>\n<tr>\n<td><strong>REPLICATE<\/strong><\/td>\n<td>Repeats a string a specified number of times<\/td>\n<\/tr>\n<tr>\n<td><strong>REVERSE<\/strong><\/td>\n<td>Reverses a string and returns the result<\/td>\n<\/tr>\n<tr>\n<td><strong>RIGHT<\/strong><\/td>\n<td>Extracts a number of characters from a string (starting from right)<\/td>\n<\/tr>\n<tr>\n<td><strong>RTRIM<\/strong><\/td>\n<td>Removes trailing spaces from a string<\/td>\n<\/tr>\n<tr>\n<td><strong>SOUNDEX<\/strong><\/td>\n<td>Returns a four-character code to evaluate the similarity of two strings<\/td>\n<\/tr>\n<tr>\n<td><strong>SPACE<\/strong><\/td>\n<td>Returns a string of the specified number of space characters<\/td>\n<\/tr>\n<tr>\n<td><strong>STR<\/strong><\/td>\n<td>Returns a number as string<\/td>\n<\/tr>\n<tr>\n<td><strong>STUFF<\/strong><\/td>\n<td>Deletes a part of a string and then inserts another part into the string, starting at a specified position<\/td>\n<\/tr>\n<tr>\n<td><strong>SUBSTRING<\/strong><\/td>\n<td>Extracts some characters from a string<\/td>\n<\/tr>\n<tr>\n<td><strong>TRANSLATE<\/strong><\/td>\n<td>Returns the string from the first argument after the characters specified in the second argument are translated into the characters specified in the third argument.<\/td>\n<\/tr>\n<tr>\n<td><strong>TRIM<\/strong><\/td>\n<td>Removes leading and trailing spaces (or other specified characters) from a string<\/td>\n<\/tr>\n<tr>\n<td><strong>UNICODE<\/strong><\/td>\n<td>Returns the Unicode value for the first character of the input expression<\/td>\n<\/tr>\n<tr>\n<td><strong>UPPER<\/strong><\/td>\n<td>Converts a string to upper-case<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<div id=\"midcontentadcontainer\">\n<div id=\"snhb-mid_content-0\"><\/div>\n<\/div>\n<hr \/>\n<h2>SQL Server Math\/Numeric Functions<\/h2>\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th><strong>Function<\/strong><\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><strong>ABS<\/strong><\/td>\n<td>Returns the absolute value of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>ACOS<\/strong><\/td>\n<td>Returns the arc cosine of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>ASIN<\/strong><\/td>\n<td>Returns the arc sine of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>ATAN<\/strong><\/td>\n<td>Returns the arc tangent of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>ATN2<\/strong><\/td>\n<td>Returns the arc tangent of two numbers<\/td>\n<\/tr>\n<tr>\n<td><strong>AVG<\/strong><\/td>\n<td>Returns the average value of an expression<\/td>\n<\/tr>\n<tr>\n<td><strong>CEILING<\/strong><\/td>\n<td>Returns the smallest integer value that is &gt;= a number<\/td>\n<\/tr>\n<tr>\n<td><strong>COUNT<\/strong><\/td>\n<td>Returns the number of records returned by a select query<\/td>\n<\/tr>\n<tr>\n<td><strong>COS<\/strong><\/td>\n<td>Returns the cosine of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>COT<\/strong><\/td>\n<td>Returns the cotangent of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>DEGREES<\/strong><\/td>\n<td>Converts a value in radians to degrees<\/td>\n<\/tr>\n<tr>\n<td><strong>EXP<\/strong><\/td>\n<td>Returns e raised to the power of a specified number<\/td>\n<\/tr>\n<tr>\n<td><strong>FLOOR<\/strong><\/td>\n<td>Returns the largest integer value that is &lt;= to a number<\/td>\n<\/tr>\n<tr>\n<td><strong>LOG<\/strong><\/td>\n<td>Returns the natural logarithm of a number, or the logarithm of a number to a specified base<\/td>\n<\/tr>\n<tr>\n<td><strong>LOG10<\/strong><\/td>\n<td>Returns the natural logarithm of a number to base 10<\/td>\n<\/tr>\n<tr>\n<td><strong>MAX<\/strong><\/td>\n<td>Returns the maximum value in a set of values<\/td>\n<\/tr>\n<tr>\n<td><strong>MIN<\/strong><\/td>\n<td>Returns the minimum value in a set of values<\/td>\n<\/tr>\n<tr>\n<td><strong>PI<\/strong><\/td>\n<td>Returns the value of PI<\/td>\n<\/tr>\n<tr>\n<td><strong>POWER<\/strong><\/td>\n<td>Returns the value of a number raised to the power of another number<\/td>\n<\/tr>\n<tr>\n<td><strong>RADIANS<\/strong><\/td>\n<td>Converts a degree value into radians<\/td>\n<\/tr>\n<tr>\n<td><strong>RAND<\/strong><\/td>\n<td>Returns a random number<\/td>\n<\/tr>\n<tr>\n<td><strong>ROUND<\/strong><\/td>\n<td>Rounds a number to a specified number of decimal places<\/td>\n<\/tr>\n<tr>\n<td><strong>SIGN<\/strong><\/td>\n<td>Returns the sign of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>SIN<\/strong><\/td>\n<td>Returns the sine of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>SQRT<\/strong><\/td>\n<td>Returns the square of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>SQUARE<\/strong><\/td>\n<td>Returns the square of a number<\/td>\n<\/tr>\n<tr>\n<td><strong>SUM<\/strong><\/td>\n<td>Calculates the sum of a set of values<\/td>\n<\/tr>\n<tr>\n<td><strong>TAN<\/strong><\/td>\n<td>Returns the tangent of a number<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>SQL Server Date Functions<\/h2>\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th><strong>Function<\/strong><\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><strong>CURRENT_TIMESTAMP<\/strong><\/td>\n<td>Returns the current date and time<\/td>\n<\/tr>\n<tr>\n<td><strong>DATEADD<\/strong><\/td>\n<td>Adds a time\/date interval to a date and then returns the date<\/td>\n<\/tr>\n<tr>\n<td><strong>DATEDIFF<\/strong><\/td>\n<td>Returns the difference between two dates<\/td>\n<\/tr>\n<tr>\n<td><strong>DATEFROMPARTS<\/strong><\/td>\n<td>Returns a date from the specified parts (year, month, and day values)<\/td>\n<\/tr>\n<tr>\n<td><strong>DATENAME<\/strong><\/td>\n<td>Returns a specified part of a date (as string)<\/td>\n<\/tr>\n<tr>\n<td><strong>DATEPART<\/strong><\/td>\n<td>Returns a specified part of a date (as integer)<\/td>\n<\/tr>\n<tr>\n<td><strong>DAY<\/strong><\/td>\n<td>Returns the day of the month for a specified date<\/td>\n<\/tr>\n<tr>\n<td><strong>GETDATE<\/strong><\/td>\n<td>Returns the current database system date and time<\/td>\n<\/tr>\n<tr>\n<td><strong>GETUTCDATE<\/strong><\/td>\n<td>Returns the current database system UTC date and time<\/td>\n<\/tr>\n<tr>\n<td><strong>ISDATE<\/strong><\/td>\n<td>Checks an expression and returns 1 if it is a valid date, otherwise 0<\/td>\n<\/tr>\n<tr>\n<td><strong>MONTH<\/strong><\/td>\n<td>Returns the month part for a specified date (a number from 1 to 12)<\/td>\n<\/tr>\n<tr>\n<td><strong>SYSDATETIME<\/strong><\/td>\n<td>Returns the date and time of the SQL Server<\/td>\n<\/tr>\n<tr>\n<td><strong>YEAR<\/strong><\/td>\n<td>Returns the year part for a specified date<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>SQL Server Advanced Functions<\/h2>\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th><strong>Function<\/strong><\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><strong>CAST<\/strong><\/td>\n<td>Converts a value (of any type) into a specified datatype<\/td>\n<\/tr>\n<tr>\n<td><strong>COALESCE<\/strong><\/td>\n<td>Returns the first non-null value in a list<\/td>\n<\/tr>\n<tr>\n<td><strong>CONVERT<\/strong><\/td>\n<td>Converts a value (of any type) into a specified datatype<\/td>\n<\/tr>\n<tr>\n<td><strong>CURRENT_USER<\/strong><\/td>\n<td>Returns the name of the current user in the SQL Server database<\/td>\n<\/tr>\n<tr>\n<td><strong>ISNULL<\/strong><\/td>\n<td>Return a specified value if the expression is NULL, otherwise return the expression<\/td>\n<\/tr>\n<tr>\n<td><strong>ISNUMERIC<\/strong><\/td>\n<td>Tests whether an expression is numeric<\/td>\n<\/tr>\n<tr>\n<td><strong>NULLIF<\/strong><\/td>\n<td>Returns NULL if two expressions are equal<\/td>\n<\/tr>\n<tr>\n<td><strong>SESSION_USER<\/strong><\/td>\n<td>Returns the name of the current user in the SQL Server database<\/td>\n<\/tr>\n<tr>\n<td><strong>SESSIONPROPERTY<\/strong><\/td>\n<td>Returns the session settings for a specified option<\/td>\n<\/tr>\n<tr>\n<td><strong>SYSTEM_USER<\/strong><\/td>\n<td>Returns the login name for the current user<\/td>\n<\/tr>\n<tr>\n<td><strong>USER_NAME<\/strong><\/td>\n<td>Returns the database user name based on the specified id<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server has many built-in functions. SQL Server Functions This reference contains string, numeric, date, conversion, and some advanced functions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}