Wednesday, May 14, 2014

Predefined Functions in sql server

Sql server provides predefined functions which are collectively call it as a built in functions of sql server are classified into
    Scalar function
    Aggregate function
    Ranking function

Scalar function

Are the built in function that operate on single value based on the data type on which scalar function operate , they are classified into
  Numeric function
  Character function
  Conversion function

Numeric function or mathematical functions

Abs(n)
That returns the absolute (positive) value of the specified numeric expression.

select abs(-76.54)

output:
76.54

select abs(-5.344)

output:
5.344
select abs(-10)


output: 10

select abs(sal) from emp



Floor (n)
Used to round the given n value to the nearest integer less than or equal given n.


select FLOOR(7.643)

output:
7

select FLOOR(-7.643)


output: -8


Ceiling(n)
Used to round the given n value to the nearest integer greater than or equal given n



ceiling(n): used to round the given  n value to the nearest integer greater than or equal given n

select CEILING(6.549)
output: 7


ROUND(n,p[,t])
used to round or truncate the given n value to p decimal places

examples

select ROUND(74.5471,2)
output: 74.5500


select round(156.567,2)
output: 156.570

select round(156.567,1)
output: 156.600





Square
Returns square of a Number
Select square(25)
Output:625
Power()
Power(m,n)
Used to calculate  power

Select power(3,3)
Output:9
Sqrt
Returns square root of given n
Select sqrt(625)
Output:25

Exp(n)
Returns exponential value of given n that is the scientific constant e raised to the power of n
Exp(n)
-e =2.718

Select exp(3) (-2.718) to the power 3
Output:20.0855

Log(n)
Returns natural logarithm of given n
Eg log(2)
0.693

Log 10(n)
Returns based 10 logarithm value of given n
Eg log10(2)
0.3010299

Radians(n)
Used to convert given n in degrees to radians
Select radians(30.00)

Degree(n)
Used to convert the given n in radiant to degrees
Select degrees(0.523555555)

Sin(n)
Returns the sin value of given n in radiants
Select sin(radians(30.00))
They will take n in radians
Output:0.5

Cos(n)
Returns the cos value of given n in radiants
Select cos(radians(30.00))
Output: 0.86

Tan(n)
Returns the tangent value of given n in radiants
Example
Tan(radians(30.00)
Output:0.577

Sign(n)
This function return zero if the given n is zero
-1 if the given n is negative and
+1 if the given n is positive.

If  n<0 it returns -1
If n=0 it returns 0
If n>0 it returns 1

Select sign(0)
Output:0
Sign(7)
Output 1
Select sign(-7)
Output -1

Pi
Select pi()
Output:3.14

Character functions or string functions

Are the built in functions that operate on character type of data.
Character functions available in sql server are as follows

Ascii(char)
Returns ascii value of given character
Ascii(‘a’)
Output: 97

Char(n)
Returns character for the given ascii value
Select char(65)
Output A

Substring(String,p,n)
Used to extract n number of characters starting from position p within the given string
Select substring(‘MICROSOFT’,3,4)

Output: cros

Select substring(‘Hello’,3,3)
Output: LLO

Left(string,n)
Used to extract n no of characters from the left of the given string
Select left(‘Hello’,3)

Write a query to get the details of employee whose name contains the first 2 characters as ‘VE’
Select * from emp where left(ename,2)=’VE’

Write a query to get the dtails of employee whose name starts with ‘s’
Select * from emp where left(ename,1)=’s’

Right(string,n)
Used to extract n no of characters from the right of the given string
Select right(‘microsoft’,4)
Output: soft

Select right(‘hello’,3)
Output: llo

Write a query to get the details  of employee whose name ends with character ‘TT’
Select * from emp where right(ename,2)=’TT’

Write a query to get the details of employees whose name 3 rd and 4 rd character are ‘TI
Select * from emp where right(left(ename,4,2)=’TI’
OR
Select * from emp where substring(ename,3,2=’TI’

Stuff(string1, p, n string2)
Used to replace n no of characters starting from position p in string1 with string2

Select stuff(‘Notepad’,1,4,’Word’)
Output: wordpad

Len(String)
Returns no of characters in the given string

Find the employees whose name contains exactly 4 characters
Select * from emp where len(ename)=4

Select len(‘hello’) output 5
Select len(‘   hello’) output 8

Reverse(string)
Used to reverse the given string
Select ename,reverse(ename) from emp

Nchar(n)
Returns the Unicode character with the specified integer code ranging between 0 t0 65,535 as defined by Unicode standard



charIndex(string1,string2[,p])
used to search for sting1 in string 2 and returns a positive value, if string1 was found in string2 and otherwise returns zero

select charIndex('o','hello world')
output 5

here the third argument specifies the position from where to start the search ,which is by default set to 1

select charIndex('or','corporation')
output: 2

select charIndex('o','hello world',6)

output: 8

find the employees whose name contains alphabet a exactly twice in the name

select * from emp where CHARINDEX('a',ename,charindex('a',ename)+1)>0



upper(String)
Returns a character expression after converting the given character data into upper case
select upper('hello')
output: HELLO

lower(string)
returns a character expression after converting the given data into lower case
select lower('HELLO')
OUTPUT: hello

RTRIM(S)
used to delete all blank spaces available on the right of the given string

select RTRIM('HELLO ')+   'WORLD'

output: HELLO WORLD


LTRIM(S)
Used to delete all blank spaces available on the left of the given string
select len(LTRIM(  'hELLO')
output: 5

space()
this function is used to generate "spaces"
select 'hello' +space(5) +'welcome'
output: hello   welcome


Replicate(s,n)
Repeats the expression 's' for specified 'n' no of times.
select Repicate('HEL',2)
OUTPUT: HELHEL


Replace()
Is used to replace one string with another string.

syntax:
Replace(string,search string,replace string)

select replace('hello','ell','abc')
output: habco

soundex(s)
returns a hexadecimal value indicating how the given string will be prounced

find the employee whose name is 'scott'

select * from emp where soundex(ename)=soundex('skott')





Delete and Truncate Command

Delete Command
Truncate Command
Delete command supports where clause and hence it can be used for deleting only
It does not support where clause and hence, it can be used only when you want to delete all rows from the table.
Delete command deletes rows one by one
It deletes the rows page by page Hence deleting all the rows from table, will be fast with truncate , compared to Delete
When there is identity on a table and you delete all rows the table using Delete than the current value of identity will not be reset to it seed
When there is identity on a table and you delete all rows from table, using truncate command, then the current value of identity will be reset to it seed.
Slow in execution
Fast in execution
Restoring is possible
Restoring is not possible
Delete command will not deallocate the memory after deleting the rows, hence delete command comes under data manipulation language
It will deallocate the memory after deleting the data by keeping one extend for the table, hence truncate comes under data definition language


Friday, May 9, 2014

Working with Rdlc Reports using Web Application.

Design the Form as following


Default.aspx

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="783px"></rsweb:ReportViewer>
 </div>

 </form>

Go to Visual Studio.

Add New Item




Select Dataset and Give the Name to Dataset.




Dataset will be displayed as below



Right Click On Dataset  and select

Add->Data Table







And Add the Columns.



Write the following Code in Default.aspx.cs

if (!IsPostBack)
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
            Products dsCustomers = GetData("select  ID,ProductCode,ContractDescriptionURL,ProductAvailableForSale from TblProducts");
            ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
            ReportViewer1.DataBind();
        }





Write the following Method in Default.aspx.cs Page


private Products GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["testConnect"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (Products dsCustomers = new Products())
                {

                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }



Again, Go to Visual Studio and Select 
Add->New Item.




And Click on Add Button.




From the Tool Box select Table.





The following screen will be displayed on Selecting the Table.




Select the Data Source-> Products( this name has been given to Dataset)








And click on Ok Button. The below screen will be visible.





Now, go to Data and give the column name.








Run the Program , and see the output.




Note: In case if u get any error as
  • A data source instance has not been supplied for the data source 'DataSet1'.


To solve this Error:

ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);

Check whether these name should be same as Data Source Name.


Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...