Space function in SQL Server

Few weeks back, I wrote a blog on CONCAT string function in SQL Server 2012. A user asked if the CONCAT function is similar to Space function in SQL Server and what is the difference between the two functions. I replied to him and clarified the difference of the 2 functions. After that I thought, it would be good to write a blog post on Space function too.

Space function

Space function is useful to return ‘x’ number of spaces, where ‘x’ is an integer.  The syntax for the space function is space(x) where x is an integer. To understand the function lets us look a simple script with and without space function.

Without Space function

The below screen shot displays an output where 2 string values are concatenated without using space function and the resulting string is an output that may not be acceptable. See that the resulting string is a single string without any break between first and last name.

Using Space function

The below screen shot displays an output where 2 string values are concatenated and separated by space function. Using space(1) generated a single space (1 space) and when that is used in the string concatenation, output string is now better and acceptable. 

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru to know when there is new content.

– Bru Medishetty

Square – Mathematical function in SQL Server

In this blog post we shall learn a mathematical function in SQL Server called Square. This function expects 1 value to be passed and returns the multiplied value itself nothing but the square of the passed value. 

The function expects a value of float data type to be passed. Any other data types, such as an integer will be implicitly converted into a float value. See that the function treats 10 (integer) and 120.59 (float) as float and returns the expected square values..

If the passed value is a integer or a float value with quotes, then that is also converted into a float. See that the function converts ’120.5′ to float 120.5 without issues.

When the value passed is a datetime or string characters (a – z) or special characters, the could not be converted to float, the function returns an error. See the error below, where ‘abcd’ is passed to the function.

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru so that, you know when there is a new blog post.

– Bru Medishetty