Get precision and decimal value in SQL Server by using PARSENAME

In SQL server there are decimal and money data type to store precision and scale both together or say decimal values. Suppose we want to get precision and scale separately then how to get it.

There are many ways to do this but there is a function in SQL named PARSENAME which can be used to get our result, see this

Declare @price Decimal(10,2)
Set @price=12345678.90
Select PARSENAME(@price,2) AS [Precision], PARSENAME(@price,1) AS [Scale]

OUTPUT:    
Precision      Scale
------------   -------
12345678       90 

Syntax: PARSENAME ( 'objectname' , objectpiece )

Actually it used to get four part of an object separated by dot,

  • object_piece 1: return last part of object
  • object_piece 2: return third part of object
  • object_piece 3: return second part of object
  • object_piece 4: return first part of object

Say we have a string "100.200.300.400" and want to separate them then

Declare @str varchar(100)
Set @str = '100.200.300.400'
Select PARSENAME(@str, 4) [First]
   , PARSENAME(@str, 3) [Second]
   , PARSENAME(@str, 2) [Third]
   , PARSENAME(@str, 1) [Fourth]

OUTPUT
First  |   Second  |  Third  |  Fourth
----------------------------------------
100    |    200    |  300    |   400

PARSENAME function can be used in many situations, say you have an IP address and you want to separate them then it will be very handy, other cases may be to get object name, schema name and database name from complete database and object name say 'master.dbo.person' etc.

Myghty .Net developer
  • sql server
  • parsename
By Myghty On 26 Mar, 13  Viewed: 1,764

Other blogs you may like

Adding identity to an existing column in SQL Server

We can not directly change a column to identity columns, say we have a primary key column or any normal column in our table and want to make it identity column then we cannot do it directly by altering the table but there are only two way to change column to identity column 1. Create a new... By Ali Adravi   On 02 May 2013  Viewed: 606

some useful sql tricks

There are many ways which can help us to save our time to write some repeated code or to do some repeated action, in this post we will see some very common code which we need very frequently in our development life. I am not saying that after this post you will save your half say work every day but... By Ali Adravi   On 31 Mar 2013  Viewed: 670

Available wildcards in sql server with examples

To write a good performing procedure or function we should be aware about available wildcards in SQL Server. I found most developer depends on ‘%’ wildcard only while we have others as well. In this article we will discuss all the available wildcards in detail with examples. First of all let’s see... By Dev D   On 25 Feb 2013  Viewed: 453

Concatenate multiple columns in SQL Server with NULL value

When we need to concatenate two columns simply we can use `+` sign and that is correct, but what if any of them is null, Will it return what we want, NO, it will return null. So let's discuss how we should concatenate two or more columns without creating extra space or extra comma problem. Let's... By Ali Adravi   On 13 Feb 2013  Viewed: 8,398

Error cannot be caught by a TRY…CATCH Construct in SQL Server

TRY…CATCH constructs do not trap the following conditions: - Warnings or informational messages that have a severity of 10 or lower. - Errors that have a severity of 20 or higher those stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of... By Ali Adravi   On 28 Dec 2012  Viewed: 440