Monday, May 2, 2016

Data types in c#



In C# data types are categories in the following types:
Value Type
Reference Type
Pointer Type

Value type – Value type variables can be assign a value directly. The following table shows the value types in c#

Type
Represents
Range
Default Value
bool
Boolean value
True or false
False
byte
8-bit unsigned integer
0 to 255
0
char
16-bit Unicode character
U +0000 to U +ffff
‘\0’
decimal
128 bit data type
-1.0 X 10-7.9X1028
0.0M
double
64 bit double-precision value
(+/-)5.0 x 10-324 to (+/-)1.7 x 10308
0.0D
float
32 bit single precision value
-3.4 x 1038 to + 3.4 x 1038
0.0F

int
32-bit signed integer type
-2,147,483,648 to 2,147,483,647
0
long
64 –bit signed integer type
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0L
sbyte
8-bit signed integer type
-128 to 127
0
short
16-bit signed integer type
-32768 to 32768
0


Reference Type – the reference type do not contain the data directly, while they contain the reference to the variable. The build-in reference type are : object, and string.

Object type – The object types can be assigned values of any other types, value types, reference types, when we assign value it need conversion ,conversion of value type to reference type is called boxing and reference type to value type is called unboxing.

Obj o;
O=10; this is called boxing

String Types - string type assign any string value to a variable
For Example – string s=”Dot Net darpan”;

Pointer type – pointer type variables store the address of another variable. Syntax of pointer type is given below:

type* identifier;

For Example –
Char* dotnet;
int * darpan;




Sunday, May 1, 2016

Group Functions in SQL

These functions take a group of rows into consideration and returns a single value as an output.

Count (*)

It returns the no. of rows that can be retrieved by the given 'select' statement.

Example

select Count (*) from Emp where depno = 30
select Count (*) from Emp where Job = 'Clerk'

Count (colname)

It returns the no. of Not Null values that are present in the specified column.

Example
select Count (comm) from Emp
select Count (M62) from Emp

NOTE

All the Group functions will not take Null values into consideration.

Sum (colname)

It returns the sum of the specified column.

Example

select Sum (sal) from Emp
select Sum (comm) from Emp

Avg (colname)

It returns the average of the specified column i.e., Sum(colname) / Count(colname)

Example

select Avg (sal) from Emp
select Avg (comm) from Emp

Max (colname)

It returns the highest value of the specified column.

Example

select Max (sal) from Emp

Min (colname)

It returns the least value of the specified column. 

Example

select Min (sal) from Emp

Stdev (colname)

It returns the standard deviation of the specified column.

Example

select Stdev (sal) from Emp

Var (colname)

It returns the variance of the specified column.

Example

select Var (sal) from Emp

Distinct function

It returns the values of the specified column by eliminating duplicates.

Example


select Distinct job from Emp

Sunday, April 24, 2016

Max and min Methods in c#



Max Method – Max method return larger value, max methods receives two numbers and return larger of the two arguments. The main advantage of using max method is that we can replace many if-statements with one line of code.

Example : -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Max());
            Console.ReadKey();

        }
    }
}

Output : 2
Min Methods – Min method is used to find the minimum value from the collection .The example of the min method is given below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Min());
            Console.ReadKey();

        }
    }
}

Output : -3

Case In SQL

In this article I am going to explain how to use case statement in sql.

Example 1

Case <Exp>
when <Match1> then <Result1>
when <Match2> then <Result2>
else <else Result>
END

If the Exp = Match1, the output will be ‘Result1’ or else checks with the other matches and returns the corresponding Results. If it is not equal to any match the output will be 'else Result'.
 

Example

select EMPNO, Job, (Case Job
                     when 'President' then 'BIGBOSS'
                     when 'Manager' then 'BOSS'
                     when 'Analyst' then 'SCIENTIST'
                     else  'Employee'
                           End )  as Comments from Emp

OutPut



Question Write a query to generate the comments based on the salary which should be as following:
If Sal < 3000 Comment should be 'Below Target'.
If Sal = 3000 Comment should be 'On Target'.
If Sal > 3000 Comment should be 'Above Target'.

Answer

select Empno, Sal, (Case Sign (Sal-3000)
                when 1 then 'Above Target'
                when 0 then 'On Target'
                when -1 then 'Below Target'
            End )  as Comments from Emp


Example 2

Case
when <condition> then <Result>
when <condition> then <Result>
else <else Result>
End
 
select Empno, Sal (Case 
    when Sal < 3000 then 'Below Target'
                               when Sal = 3000 then 'On Target'
                               when Sal > 3000 and Sal <= 1000   then 'Above Target'
                               when Sal > 5000 then 'Very High Target'
                         End ) as Comments from Emp

 

Wednesday, April 20, 2016

Operators Precedence in C #


Category
Operator
Associativity
Postfix
()[]->.++--
Left to right
Unary
+-!~++--(type)*sizeof
Right to left
Multiplicative
*/%
Left to right
Additive
+-
Left to right
Shift
<<>> 
Left to right
Relational
<< = >> =
Left to right
Equality
==!=
Left to right
Bitwise AND
&
Left to right
Bitwise XOR
^
Left to right
Bitwise OR
|
Left to right
Logical AND
&&
Left to right
Logical OR
||
Left to right
Conditional
?:
Right to Left
Assignment
=+=-=*=/=%=>>=<<=&=^=|=
Right to left
Comma
,
Left to right