Demonstration of SQL Functions

Posted by Abhisek under Sql Server category on | Views : 1831
This script file contains simple sql commands. It include commands to create a database, create a table, inserting values to table, changing column of a table. It also demonstrates the use of some functions such as power, abs, datepart, date diff, min substring.

It will be useful for newcomers to sql.

create database itm
create table itm(name varchar(20),roll int,mark int,phoneno char(10))
insert itm values('lipu',41,77,9861205901)
alter table itm alter column phoneno char(10)
sp_help itm
select*from itm
insert itm (roll,mark)values(108,78)
update itm set name='abhi' where roll=108
select name,roll from itm where mark=78
select substring('lipunmqwer',5,3)
select abs (12)
select power(2,3)
select min(mark)from itm
select datepart(yy,getdate())
select datediff(yy,'2005-07-12','2008-07-12')

Descriptin of commands:-

power(a,b)- calculates a^b

abs()- shows the absolute value

min(values)- selects minimum of a given range of values

substring(string,start,end)- extract the substring according to the range passed as start and end from the given string

getdate()- it will show the current date

datepart(parameter,date)- will extract the date part according to the parameter passed. The parameters can be yy for year, mm for month or dd for date.

datediff(parameter,date1,date2)- will extract the difference between two given dates as per the parameter passed.

Comments or Responses

Login to post response