Wednesday, 14 November 2012

Techniques for adding the numbers

1.Techniques for adding the numbers 1 to 100

Sum from 1 to n =\displaystyle{\frac{n(n+1)}{2}}


Sum from 1 to 100 = \displaystyle{\frac{100(100+1)}{2} = (50)(101) = 5050}

2.Let's prove the second statement.

1 + 22 + 32 + ... + n2 =

3.The sum of cubes

 
\begin{eqnarray*} 
1 + 2 + 3 + 4 + \ldots + n & = & \frac{n(n + 1)}{2} \\ 
1^3 + 2^3 + 3^3 + 4^3 + \ldots + n^3 & = & \left[\frac{n(n + 1)}{2}\right]^2 
\end{eqnarray*} 
  


Wednesday, 7 November 2012

print a triangle of stars using SQL Server


DECLARE @count INT,@num INT,@num1 INT, @space INT, @str varchar(50)
SET @count =10 SET @num = 1
WHILE(@num<=@count)
BEGIN
    SET @num1 = 0 SET @space = @count-@num
    WHILE (@num1<@num)
    BEGIN
        if @str is null
            SET @str = '* '
        ELSE
            SET @str = @str+'* '   SET @num1 = @num1+1
    END
    PRINT (space(@space)+@str)
    SET @num = @num+1   SET @str = null
END

Find Nth column of the Particular Table – Query to Retrieve the Nth value

SELECT TOP 1 *
    FROM (
            SELECT TOP 3 *,ROW_NUMBER() OVER ( ORDER BY ColumnNameBasedOnWhichOrder DESC) AS RowNum
            FROM TableName
        )a 
ORDER BY
    a.RowNum ASC

SELECT TOP 1 ColumnDetailID
FROM (
SELECT DISTINCT TOP 6 ColumnDetailID
FROM TableName
ORDER BY ColumnDetailID DESC) a
ORDER BY ColumnDetailID


SELECT TOP 1 ColumnDetailID
FROM
(
    SELECT TOP 3 ColumnDetailID
    FROM TableName
    ORDER BY ColumnDetailID DESC
) SalarySubquery
ORDER BY ColumnDetailID ASC

Monday, 22 October 2012

Diff between SQL Server 2005 and SQL Server 2008

"What is the diff. between Sqlserver 2005 and Sqlserver 2008"?
Here I am going to describe only 3 differences which is related to Declare and Set Keywords of Sql.
Differences 
1. You can declare variable and assign the value without using Set keyword in sqlserver 2008. / but not in 2005
2. You can increment the value like c# @i+=1 in sqlserver 2008. / but not in sqlserver 2005
3. You can put the break point for debug query  in sqlserver 2008. / but not in sqlserver 2005
...many other diff but here is only 3 we describing 
Declare @I int = 1
while(@I<=10)
begin
print @i
set @i+=1
end

Computed Column – PERSISTED and Storage


If we are creating one computed column in the database it won't take more size of that computed column until we give the PERSISTED keyword

It does not increase the data size of the original table as well as it does not turn the column into persisted. When we create an index on the column of the table, there is an additional space occupied by that index.

More Details:
http://blog.sqlauthority.com/2010/08/21/sql-server-computed-column-persisted-and-storage-part-2/

Monday, 24 September 2012

Simple DataSet Sorting in C#

           Simple DataSet Sorting in C# 

            DataSet dsTest = new DataSet();
            DataTable dtSample = new DataTable("SampleTable");
            DataColumn dcName = new DataColumn("Name",typeof(string));
            dtSample.Columns.Add(dcName);
            dtSample.Columns.Add("Age", (typeof(Int32)));
            dtSample.Rows.Add("Lawrence", 25);
            dtSample.Rows.Add("Dinesh", 30);
            dtSample.Rows.Add("Aruna", 24);
            dtSample.Rows.Add("Deb", 35);
            dtSample.Rows.Add("Satish", 42);
            DataView dvSample = new DataView(dtSample);
            dvSample.RowFilter = "Age <> 30";
            dvSample.Sort = "Age Asc";
            dsTest.Tables.Add(dtSample);

Adding One more column with existing DataTable

               object objCheckAmount;
                if (ReceivableDT.Columns.Contains("CheckAmountWithTax") == false)
                {
                    ReceivableDT.Columns.Add("CheckAmountWithTax", typeof(System.Double), "Paid + TaxPaid");
                }
                objCheckAmount = ReceivableDT.Compute("Sum(CheckAmountWithTax)", "Select = 'true' and ReceivableHeaderID='" + ReceivableHeaderID + "'");
                if (objCheckAmount != DBNull.Value)
                {
                    CheckAmount = Convert.ToDouble(objCheckAmount);
                }

Thursday, 30 August 2012

Rounding Functions IN SQL Server, C# and JavaScript


/// SQL Query to Round the values in different format

 DECLARE @value decimal(18,5)
SET @value = 6.001
SELECT ROUND(@value, 1)
SELECT CEILING(@value)
SELECT FLOOR(@value)


/// C# code to Round the values in different format

decimal Value = Convert.ToDecimal(Console.ReadLine());
System.Console.WriteLine("RoundValue={0}\nCeilingValue={1}\nFloorValue={2}", Math.Round(Value),Math.Ceiling(Value),Math.Floor(Value));

/// JavaScript code to Round the values in different format

var ceilValue = Math.ceil(Value); 
var floorValue = Math.floor(Value);
var
roundValue = Math.round(Value);