{"id":870,"date":"2018-10-07T11:07:29","date_gmt":"2018-10-07T04:07:29","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=870"},"modified":"2020-04-11T20:13:08","modified_gmt":"2020-04-11T13:13:08","slug":"sql-server-scalar-functions","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/","title":{"rendered":"SQL Server Scalar Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about SQL Server scalar functions and how to use them to encapsulate formulas or business logic and reuse them in the queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-are-scalar-functions'>What are scalar functions <a href=\"#what-are-scalar-functions\" class=\"anchor\" id=\"what-are-scalar-functions\" title=\"Anchor for What are scalar functions\">#<\/a><\/h2>\n\n\n\n<p>SQL Server scalar function takes one or more parameters and returns a single value.<\/p>\n\n\n\n<p>The scalar functions help you simplify your code. For example, you may have a complex calculation that appears in many <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">queries<\/a>. Instead of including the formula in every query, you can create a scalar function that encapsulates the formula and uses it in each query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-scalar-function'>Creating a scalar function <a href=\"#creating-a-scalar-function\" class=\"anchor\" id=\"creating-a-scalar-function\" title=\"Anchor for Creating a scalar function\">#<\/a><\/h2>\n\n\n\n<p>To create a scalar function, you use the <code>CREATE FUNCTION<\/code> statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> &#91;schema_name.]function_name (parameter_list)\n<span class=\"hljs-keyword\">RETURNS<\/span> data_type <span class=\"hljs-keyword\">AS<\/span>\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    statements\n    <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-keyword\">value<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, specify the name of the function after the <code>CREATE FUNCTION<\/code> keywords. The schema name is optional. If you don&#8217;t explicitly specify it, SQL Server uses <code>dbo<\/code> by default.<\/li><li>Second, specify a list of <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-stored-procedure-parameters\/\">parameters<\/a> surrounded by parentheses after the function name.<\/li><li>Third, specify the data type of the return value in the <code>RETURNS<\/code> statement.<\/li><li>Finally, include a <code>RETURN<\/code> statement to return a value inside the body of the function.<\/li><\/ul>\n\n\n\n<p>The following example creates a function that calculates the net sales based on the quantity, list price, and discount:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> sales.udfNetSale(\n    @quantity <span class=\"hljs-built_in\">INT<\/span>,\n    @list_price <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>),\n    @discount <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">2<\/span>)\n)\n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-keyword\">AS<\/span> \n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">RETURN<\/span> @quantity * @list_price * (<span class=\"hljs-number\">1<\/span> - @discount);\n<span class=\"hljs-keyword\">END<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Later on, we can use this to calculate net sales of any sales order in the <code>order_items<\/code> from the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"181\" height=\"169\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png\" alt=\"order_items table\" class=\"wp-image-159\"\/><\/figure>\n\n\n\n<p>After creating the scalar function, you can find it under <strong>Programmability &gt; Functions &gt; Scalar-valued Functions<\/strong> as shown in the following picture:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"374\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Scalar-Function.png\" alt=\"SQL Server Scalar Function\" class=\"wp-image-871\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Scalar-Function.png 483w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Scalar-Function-300x232.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='calling-a-scalar-function'>Calling a scalar function <a href=\"#calling-a-scalar-function\" class=\"anchor\" id=\"calling-a-scalar-function\" title=\"Anchor for Calling a scalar function\">#<\/a><\/h2>\n\n\n\n<p>You call a scalar function like a built-in function. For example, the following statement demonstrates how to call the <code>udfNetSale<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    sales.udfNetSale(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">100<\/span>,<span class=\"hljs-number\">0.1<\/span>) net_sale;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"73\" height=\"42\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Scalar-Function-example.png\" alt=\"SQL Server Scalar Function example\" class=\"wp-image-872\"\/><\/figure>\n\n\n\n<p>The following example illustrates how to use the <code>sales.udfNetSale<\/code> function to get the net sales of the sales orders in the <code>order_items<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    order_id, \n    <span class=\"hljs-keyword\">SUM<\/span>(sales.udfNetSale(quantity, list_price, discount)) net_amount\n<span class=\"hljs-keyword\">FROM<\/span> \n    sales.order_items\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    order_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    net_amount <span class=\"hljs-keyword\">DESC<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"127\" height=\"268\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Scalar-Function-calling-function.png\" alt=\"SQL Server Scalar Function - calling function\" class=\"wp-image-873\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='modifying-a-scalar-function'>Modifying a scalar function <a href=\"#modifying-a-scalar-function\" class=\"anchor\" id=\"modifying-a-scalar-function\" title=\"Anchor for Modifying a scalar function\">#<\/a><\/h2>\n\n\n\n<p>To modify a scalar function, you use the <code>ALTER<\/code>&nbsp;instead of the&nbsp;<code>CREATE<\/code> keyword. The rest statements remain the same:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> &#91;schema_name.]function_name (parameter_list)\n    <span class=\"hljs-keyword\">RETURN<\/span> data_type <span class=\"hljs-keyword\">AS<\/span>\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        statements\n        <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-keyword\">value<\/span>\n    <span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you can use the <code>CREATE OR ALTER<\/code> statement to create a user-defined function if it does not exist or to modify an existing scalar function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR<\/span> <span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> &#91;schema_name.]function_name (parameter_list)\n        <span class=\"hljs-keyword\">RETURN<\/span> data_type <span class=\"hljs-keyword\">AS<\/span>\n        <span class=\"hljs-keyword\">BEGIN<\/span>\n            statements\n            <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-keyword\">value<\/span>\n        <span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='removing-a-scalar-function'>Removing a scalar function <a href=\"#removing-a-scalar-function\" class=\"anchor\" id=\"removing-a-scalar-function\" title=\"Anchor for Removing a scalar function\">#<\/a><\/h2>\n\n\n\n<p>To remove an existing scalar function, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-drop-function\/\">DROP FUNCTION<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> &#91;schema_name.]function_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, to remove the <code>sales.udfNetSale<\/code> function, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> sales.udfNetSale;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-scalar-function-notes'>SQL Server scalar function notes <a href=\"#sql-server-scalar-function-notes\" class=\"anchor\" id=\"sql-server-scalar-function-notes\" title=\"Anchor for SQL Server scalar function notes\">#<\/a><\/h2>\n\n\n\n<p>The following are some key takeaway of the scalar functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Scalar functions can be used almost anywhere in T-SQL statements.<\/li><li>Scalar functions accept one or more parameters but return only one value, therefore, they must include a <code>RETURN<\/code> statement.<\/li><li>Scalar functions can use logic such as <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\">IF<\/a><\/code> blocks or <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\">WHILE<\/a><\/code> loops.<\/li><li>Scalar functions cannot <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-update\/\">update<\/a> data. They can access data but this is not a good practice.<\/li><li>Scalar functions can call other functions.<\/li><\/ul>\n\n\n\n<p>In this tutorial, you have learned how to use SQL Server scalar functions to encapsulate complex formulas or complex business logic and reuse them in queries.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"870\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/\"\n\t\t\t\tdata-post-title=\"SQL Server Scalar Functions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"870\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/\"\n\t\t\t\tdata-post-title=\"SQL Server Scalar Functions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":851,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-870","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server Scalar Functions By Practical Examples<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Scalar Functions By Practical Examples\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T13:13:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/\",\"name\":\"SQL Server Scalar Functions By Practical Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/order_items.png\",\"datePublished\":\"2018-10-07T04:07:29+00:00\",\"dateModified\":\"2020-04-11T13:13:08+00:00\",\"description\":\"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/order_items.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/order_items.png\",\"width\":181,\"height\":169,\"caption\":\"order_items\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-scalar-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server User-defined Functions\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server Scalar Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Scalar Functions By Practical Examples","description":"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Scalar Functions By Practical Examples","og_description":"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-04-11T13:13:08+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/","name":"SQL Server Scalar Functions By Practical Examples","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png","datePublished":"2018-10-07T04:07:29+00:00","dateModified":"2020-04-11T13:13:08+00:00","description":"This tutorial shows you how to use SQL Server scalar functions to use encapsulate formulas or business logic and reuse them in the queries.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/order_items.png","width":181,"height":169,"caption":"order_items"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-scalar-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server User-defined Functions","item":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/"},{"@type":"ListItem","position":3,"name":"SQL Server Scalar Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/870\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/851"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}