Search This Blog

Friday, November 22, 2013

MIN function SQL Server Example

SQL Server > Built-in Functions > MIN

MIN returns the minimum value

Warning! Null values are ignored!

Example

CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (2,'p3', null,2) ,   (2,'p4', 50,2)

select * from #LocalTempTable

select min(Salary) MIN_salary from #LocalTempTable
select min(Salary) MIN_salary, DeptId from #LocalTempTable group by DeptId

drop table #LocalTempTable
Results:

ID Name Salary DeptId
1 p1 100 1
2 p2 200 1
2 p3 NULL 2
2 p4 50 2

MIN_salary
50

MIN_salary DeptId
100 1
50 2





AVG function SQL Server Example

SQL Server > Built-in Functions > AVG

AVG returns the average of the values in a group without Null values

Example:

1. Calculate average salary global and for each department

CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (2,'p2', null,2)

select avg(Salary) avg_salary from #LocalTempTable
select avg(Salary) avg_salary, DeptId from #LocalTempTable group by DeptId

drop table #LocalTempTable
Results:
avg_salary
150
avg_salary DeptId
150            1
NULL       2
2. Average without zero (0)




UPPER SQL Server Example

SQL Server > Built-in Functions > UPPER

UPPER  converts a character expression to uppercase.

Example

SELECT UPPER('this is lowercase expression')
Result:
THIS IS LOWERCASE EXPRESSION





CHAR Function SQL Server

SQL Server > Built-in Functions > CHAR

Converts an ASCII code to a character.

Example

SELECT CHAR(68),CHAR(100)
Result:
D d




ASCII SQL Server

SQL Server > Built-in Functions > ASCII

Returns the ASCII code value of the character






Example

SELECT ASCII('D'),ASCII('d')

Result:
68 100

Thursday, November 21, 2013

Built-In Functions SQL Server

SQL Server > Built-in Functions

String
Aggregate
Conversion
Rank
Date and Time
System
Mathematical
Metadata







      Generics C#

      C# > Generics

      Generics means the concept of type parameters. In this way it is possible to design classes  that defer the specification of types until the class is declared and instantiated.
      • generic types maximize code reuse, type safety, and performance.
      • T is generic type parameter
      Links                                                                                                  

      Example: Simple generic list                                                           

      public class GenericList<T>
      {
          private List<T> l = new List<T>();
          public void Add(T element)
          {
             l.Add(element);
          }
      }

      GenericList<int> lst1 = new GenericList<int>();
      lst1.Add(1);

      GenericList<string> lst2 = new GenericList<string>();
      lst2.Add("generics");