Thursday 30 June 2011

Truncate decimals instead of Rounding from a double value In C#

Introduction

Many times while working on applications, we often land into a situation where we want to truncate the decimal places from a number instead of round like suppose we have 5.329 and I want to truncate the third digit then my required output will be 5.32. Here, I will walk through the different ways which I know in which we can achieve the same.

Approaches

I have created the approaches as making the C# method.

1. Truncate the decimals through the string operations.

Below method only usable if you want to truncate the decimal places to two.

   1: #region Round
   2: /// <summary>
   3: /// Truncate the last digit after two digit floating points.
   4: /// </summary>
   5: /// <param name="value">A double-precision floating-point number to be rounded.</param>
   6: /// <returns>Double value contains two digit floating points.</returns>
   7: public static double Round (double value)
   8: {
   9:   double returnValue = 0;
  10:  
  11:   try
  12:   {
  13:     //Convert the value into the string for round.
  14:     string input = Convert.ToString(value);
  15:  
  16:     //Check if the input contains decimal places
  17:     if (input.Contains("."))
  18:     {
  19:       //Check if there are 3 or more digits after the decimal point.
  20:       if (input.Length - input.IndexOf(".", StringComparison.Ordinal) >= 4)
  21:       {
  22:         //Replace the input value with the required value. Added 3 to get only 2 digits after the decimal point.
  23:         input = input.Substring(0, input.IndexOf(".", StringComparison.Ordinal) + 3); 
  24:       }
  25:     }
  26:         //Converts the string representation of a number to its System.Double equivalent.
  27:     returnValue = Convert.ToDouble(input);
  28:   }
  29:   catch { } //Suppress exception no need to handle.
  30:  
  31:   return returnValue;
  32: }
  33: #endregion

2. Truncate the decimals through the mathematical operations.

Through this method you can truncate any number of decimal places.


   1: #region Round
   2: /// <summary>
   3: /// Rounds a decimal value to a specified number of fractional digits.
   4: /// </summary>
   5: /// <param name="value">A decimal number to be rounded.</param>
   6: /// <param name="decimals">The number of decimal places in the return value.</param>
   7: /// <returns>The number nearest to value that contains a number of fractional digits equal to decimals.</returns>
   8: public static double Round(double value, byte decimals)
   9: {
  10:   double returnValue = 0;
  11:  
  12:   try
  13:   {
  14:     if (value != 0)
  15:     {
  16:       //Gets the power of 10 base on passed decimal parameter value (10^decimals).
  17:       double powValue = Math.Pow(10, decimals);
  18:  
  19:       //Gets the truncated value.
  20:       returnValue = Math.Truncate(CDCommon.ToDouble(value * powValue)) / powValue;
  21:     }
  22:   }
  23:   catch
  24:   { throw; }
  25:  
  26:   return returnValue;
  27: }
  28: #endregion

Conclusion

From both the ways, as per my opinion prefer the second approach in implementation because it’s very light compared to the first in terms of calculations.

Wednesday 29 June 2011

New Date and Time Functions in SQL Server 2008

Introduction

SQL Server 2008 introduces five new date and time functions namely SYSDATETIME, SYSDATETIMEOFFSET, SYSUTCDATETIME, SWITCHOFFSET and TODATETIMEOFFSET.

In this article described new functions in detail.


1)    SYSDATETIME

The SYSDATETIME function basically returns the current system timestamp without the time zone information.


SELECT SYSDATETIME()

GO

 

SYSDATETIME

------------------------

2011-06-27 16:06:15.9409129


2)    SYSDATETIMEOFFSET

The SYSDATETIMEOFFSET function is much similar to SYSDATETIME function; however it will also get you the time zone information.


SELECT SYSDATETIMEOFFSET()

GO

 

SYSDATETIMEOFFSET

-------------------------------

2011-06-27 16:07:01.8205935 +05:30

 

 

3)    SYSUTCDATETIME

The SYSUTCDATETIME function returns the Greenwich Mean Time (GMT) or the Coordinated Universal Time (UTC). This time is derived from the current local system time and the time zone settings of the server where SQL Server 2008 is running.


SELECT SYSUTCDATETIME()

GO

 

SYSUTCDATETIME

------------------------

2011-06-27 10:38:25.6239407


4)    SWITCHOFFSET

The SWITCHOFFSET function returns a DATETIMEOFFSET value that is changed from the stored time zone offset to a specified new time zone offset.


SELECT SYSDATETIMEOFFSET(),

       SWITCHOFFSET (SYSDATETIMEOFFSET(), '+00:00')

GO

 

SYSDATETIMEOFFSET                                            SWITCHOFFSET

-------------------------------                              -------------------------------------

2011-06-27 16:30:55.7127120 +05:30           2011-06-27 11:00:55.7127120 +00:00


5)    TODATETIMEOFFSET

The TODATETIMEOFFSET function returns a DATETIMEOFFSET value that is translated from a DATETIME2 expression.


SELECT TODATETIMEOFFSET (GETDATE(),'+05:30')

GO

 

TODATETIMEOFFSET

---------------------------------

2011-06-27 16:26:56.340 +05:30



Monday 20 June 2011

Rebuild all the Indexes of a SQL Database in one go

Introduction

In my last post, I had explained what could be the best value of Fill Factor for the indexes in SQL Server and had promised to show a handy way to ReBuild all the indexes including the ones created on the Indexed Views.

Implementation

USE DBName
GO
 
DECLARE @tsql NVARCHAR(MAX)  
DECLARE @fillfactor INT
 
SET @fillfactor = 70 
 
SELECT @tsql = 
  STUFF(( SELECT DISTINCT 
           ';' + 'ALTER INDEX ALL ON ' + o.name + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'
          FROM 
           sysobjects o 
          INNER JOIN sysindexes i 
           ON o.id = i.id 
          WHERE 
           o.xtype IN ('U','V')
           AND i.name IS NOT NULL
          FOR XML PATH('')), 1,1,'')
 
--PRINT @tsql          
EXEC sp_executesql @tsql  

Conclusion


This way, we can easily rebuild all the existing indexes on the Tables as well as the Indexed Views of the selected Database with an option to set the fillfactor as well. Hope, this script will prove to be handy.

Monday 13 June 2011

What is the best value for Fill Factor in SQL Server?


Introduction
When an index is created or rebuilt, the fill-factor value determines the percentage of space on each leaf-level page to be filled with data, reserving the remainder on each page as free space for future growth. For example, specifying a fill-factor value of 80 means that 20 percent of each leaf-level page will be left empty, providing space for index expansion as data is added to the underlying table. The empty space is reserved between the index rows rather than at the end of the index.

The fill-factor value is a percentage from 1 to 100, and the server-wide default is 0, which means that the leaf-level pages are filled to capacity. Fill-factor values 0 and 100 are the same in all respects. The fill-factor setting applies only when the index is created, or rebuilt.

How does it help?
The fill-factor option is provided for fine-tuning index data storage and performance. In an insert-intensive environment, the index pages will eventually split to accommodate additional entries. To avoid or reduce the frequency of page splits, the index should be rebuilt using an appropriate fill factor.

What is the ideal value?
It depends on the ratio of reads to writes that your application makes to your SQL Server tables. As a rule of thumb, we might follow these guidelines:
  •          Low Update Tables (100-1 read to write ratio): 100% fill factor
  •          High Update Tables (where writes exceed reads): 50%-70% fill factor
  •          Everything In-Between: 80%-90% fill factor.


Hope, this small article will help you in identifying the fill-factor for your application/system.

Next, I would come up with a handy script to update the fill-factor of all the existing indexes at one go.

Friday 3 June 2011

Maximum recursion possible with CTE in SQL Server 2005/2008


In my last blog about Tally Tables, we talked about the use of recursive CTEs to generate Tally Tables. Following that, someone quickly asked me to generate a Tally Table for integers starting from 1 to 150 as when he tried the way explained in my last post, the following exception was generated –

Msg 530, Level 16, State 1, Line 3
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
Now what to do?
Actually, to prevent infinite recursion, a default value for MAXRECURSION = 100 has been already set. Hence, any recursion will stop on reaching the threshold limit. If we want to loop/iterate more than the default value, we need to set the MAXRECURSION value as explained in my another post Prevent recursive CTE from entering an infinite loop
So, the following statement will work to generate a Tally Table from 1 to 150–
DECLARE @Max AS INT = 150

;WITH CTE AS (
  SELECT 1 Num
  UNION ALL
  SELECT Num + 1 FROM CTE WHERE Num < @Max
)

SELECT * FROM CTE OPTION (MAXRECURSION 150);

After looking at this option, most of us will ask – what is the max value that could be used with the MAXRECURSION option? And the answer is – 32767. If we try to set a value greater than this, 
SELECT * FROM CTE OPTION (MAXRECURSION 32768);
sql fires the following exception –
Msg 310, Level 15, State 1, Line 10
The value 32768 specified for the MAXRECURSION option exceeds the allowed maximum of 32767.

Hope, this post will make you understand few more facts related to the use of CTE with recursion.
Happy iterating…