This document contains SQL statements for creating tables, inserting and selecting data, adding constraints, using functions like date, string and math functions. It also contains statements for case, coalesce and isnull functions.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
86 views
SQL Query
This document contains SQL statements for creating tables, inserting and selecting data, adding constraints, using functions like date, string and math functions. It also contains statements for case, coalesce and isnull functions.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
select * from cust where custname='vivek'
create table ordername(oid int ,oname varchar(50) not null)
insert into ordername values('myorder6') drop table ordername create table ordername(oid int identity(1,1) primary key ,oname varchar(50) not null) create table customer(cid int identity(1,1) primary key ,oid int not null,cname varchar(50)) select * from ordername alter table customer add cage int /*To add the column*/ alter table customer alter column cage varchar(50)/* To change the type of colum n*/ sp_rename 'dbo.customer.cname','customername','COLUMN' /* To rename the column n ame of table*/ create rule rule1 as @eid<=10 create table customerpra(cid int primary key ,oid int not null,cname varchar(50) ) sp_bindrule rule1, 'customerpra.oid' sp_bindrule rule1, 'customer.cid' create default default1 as 100 sp_bindefault default1,'customerpra.oid' insert into customerpra values(200,default,'raju') select datepart(dd,getdate) as 'Today Date' alter table customer add constraint fk_oid foreign key(oid) references ordername (oid)/* Adding foreign key(fk_oid) to ordername table */ select len('Rajkumar') as 'length' select RTRIM(' guna ') as name select LTRIM(' guna') as name select right('gunanidhi',5) as 'right namme' select rtrim(substring('gunanidhi',1,5)) as 'name' select reverse('gunanidhi') select charindex('n''gunanidhi') select replace('gunanidhi','n','a') select patindex('%nani%','gunanidhi') /*gives the first occurance of required pa ttern find. */ select upper('RAJKUMAR') select lower('RAJKUMAR') as 'lower case' select 'Guna'+space(2)+'Nidhi'+space(2)+'Pokhrel' select round(125.9995,-1) select isnumeric(5) select isnumeric('c') select abs(-0) select ceiling(-1.565) /* smallest integer no greater than or equal to that no k eep walking right */ select floor(-1.569) /*keep walking left -2 -1 0 1 2 3 4 5 */
declare @num1 float
declare @num2 float set @num1=1 while @num1<=10 begin select sqrt(@num1) SELECT @num1=@num1+1 end SELECT round((rand()*10)+1,0)/* random numbers from 1 to 10*/ select month(getdate()) select year(getdate()) select day(getdate()) select convert(varchar(50),datepart(hour,getdate()))+space(2)+'Hour: '+ convert(varchar(50),datepart(minute,getdate()))+space(2)+'Minutes: '+ convert(varchar(50),datepart(second,getdate()))+space(2)+'Seconds' use Pra GO DECLARE @date1 varchar(8) set @date1='3/2/1982' if(select isdate(@date1)==1) print 'yes' else PRINT 'no'
DECLARE @datestring varchar(8)
declare @num int SET @datestring = '12/21/98' SELECT @num= ISDATE(@datestring) case @num when '1' then 'yes' when '-1' then 'NO' when '0' then 'Hi' END as 'print' SELECT oid,oname CASE oname WHEN 'myorder' THEN 'This is vendor id one' END AS PrintMessage FROM ordername Select oid,oname = case When oname='myorder2' then 'This is vendor id one' else 'This is vendor id two' end from ordername select freight= CASE WHEN freight<100 THEN 'Too less' WHEN freight>=100 THEN 'Too Heavy' end FROM orders /* COALESCE - Returns the first nonnull expression among its arguments. Syntax: COALESCE (expression [...n]) */ /* ISNULL - Replaces NULL with the specified replacement value. Syntax: ISNULL (check_expression, replacement_value) Example: use Vendor GO SELECT PostedDate, ISNULL(PostedDate, '1900-01-01') AS NewDate FROM Vendor GO */