Sie sind auf Seite 1von 18

CREATE TABLE emp_sal_3 (

empno integer
, ename varchar(10)
, sal decimal(13,4)
, comm decimal(13,7)

);
insert into emp_sal_3 values (100, 'rama', 10000.15, 100);
insert into emp_sal_3 values (200, 'mac', 20000, 150), (300, 'jorrit', 8000.75,
NULL);
insert into emp_sal_3 values (400, 'rama', 17000.22, 200);

alter table emp_sal_3 add location varchar(10);

update emp_sal_3
set location = 'BNG'
;
update emp_sal_3
set comm = 1
where empno = 300 and location = 'BNG'
;
insert into emp_sal_3 values (500, 'hans', 19000, 200, 'pune'), (600, 'harm',
19000, 200, 'delhi');

alter table emp_sal_3 alter column location varchar(20);

select empno, ename, sal, comm, location, datalength(location) from emp_sal_3;

alter table emp_sal_3 alter column location char(20);

select empno, ename, sal, comm, location, datalength(location) from emp_sal_3;

alter table emp_sal_3 alter column location varchar(20);

insert into emp_sal_3 values (700, 'ajan', 39000, 400, 'bng');

select empno, ename, sal, comm, location, datalength(location) from emp_sal_3;

select * into emp_sal17 from emp_sal_3;

drop table emp_sal17;

sp_rename 'emp_sal_3', 'emp_sal7';

sp_rename 'emp_sal7.location', 'loc', 'column';

alter table emp_sal7 drop column loc;

select * into emp_sal_3 from emp_sal7;

delete from emp_sal7 where ename = 'rama' or comm = 1 or sal = 39000;

delete from emp_sal7;

select * into emp_sal8 from emp_sal7;

truncate table emp_sal7;


drop table emp_sal8;

insert into emp_sal7 values (100, 'rama', 10000.15, 100),


(200, 'mac', 20000, 150),
(300, 'jorrit', 8000.75, NULL),
(400, 'rama', 17000.22, 200);

Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values


(7369,'SMITH','CLERK',7902,('17-DEC-80'),800,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7499,'ALLEN','SALESMAN',7698,('20-FEB-81'),1600,300,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7521,'WARD','SALESMAN',7698,('22-FEB-81'),1250,500,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7566,'JONES','MANAGER',7839,('02-APR-81'),2975,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7654,'MARTIN','SALESMAN',7698,('28-SEP-81'),1250,1400,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7698,'BLAKE','MANAGER',7839,('01-MAY-81'),2850,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7782,'CLARK','MANAGER',7839,('09-JUN-81'),2450,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7788,'SCOTT','ANALYST',7566,('19-APR-87'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7839,'KING','PRESIDENT',null,('17-NOV-81'),5000,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7844,'TURNER','SALESMAN',7698,('08-SEP-81'),1599,999,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7876,'ADAMS','CLERK',7788,('23-MAY-87'),1100,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7900,'JAMES','CLERK',7698,('03-DEC-81'),950,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7902,'FORD','ANALYST',7566,('03-DEC-81'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values
(7934,'MILLER','CLERK',7782,('23-JAN-82'),1300,null,10);

select count(*), sum(sal), avg(sal), max(sal), min(sal) from emp;

select deptno, sum(sal) from emp group by deptno;

select deptno, sum(sal)


from emp
where deptno <> 20
group by deptno
having sum(sal) > 9000;

select deptno, sal, empno, ename from emp where deptno <> 10;

select deptno, sal, empno, ename from emp where deptno <> 10 order by deptno;

select deptno, sal, empno, ename from emp order by deptno where deptno <> 10;

select deptno, sum(sal) sal from emp where deptno <> 10 group by empno;

select deptno, sum(sal) sal from emp group by deptno where deptno <> 10;

select deptno, sum(sal) sal from emp where deptno <> 10 having sum(sal) > 4000;

select deptno, sum(sal) sal from emp where deptno <> 10 group by deptno having
sum(sal) > 4000;

select deptno, sum(sal) sal from emp where deptno <> 10 having sum(sal) > 4000
group by deptno;

select deptno, sum(sal) sal from emp where deptno <> 10 order by deptno having
sum(sal) > 4000 group by deptno ;

select empno, ename, sal, comm, sal+comm from emp;

select empno, ename, sal, comm, sal+isnull(comm, 0) from emp;

select empno, ename, sal, comm, sal+isnull(comm, 1) from emp;

select empno, ename, sal, comm, sal+isnull(comm, 10000) from emp;

select empno, ename, sal, comm, sal+isnull(comm, 100000) from emp; --error

select empno, ename, sal, comm, sal+isnull(comm) from emp; --error

select empno, ename, sal, comm from emp where comm = NULL;

select empno, ename, sal, comm from emp where comm IS NULL;

select empno, ename, sal, comm from emp where comm IS not NULL;

select coalesce (-23, 45, 0, 1, 1.9, 67, 89, 100) ;


select coalesce (0, 45, 0, 1, 1.9, 67, 89, 100) ;
select coalesce (NULL, 45, 0, 1, 1.9, 67, 89, 100) ;

create table tablea (col1 INTEGER);


create table tableb (colx INTEGER);

insert into tablea values (5);


insert into tablea values (1);
insert into tablea values (20);
insert into tablea values (6);
insert into tablea values (NULL);

insert into tableb values (4);


insert into tableb values (1);
insert into tableb values (10);
insert into tableb values (6);
insert into tablea values (NULL);
insert into tableb values (18);

select col1 from tablea


UNION
select colx from tableb;

select col1 from tablea


UNION ALL
select colx from tableb;

select col1 from tablea


EXCEPT
select colx from tableb;
select col1 from tablea
INTERSECT
select colx from tableb;

select abs(-16.78);

select abs(-998.7878956) ;

select abs(123416.786789) ;

select abs(0) ;

select abs(NULL) ;

select sign(-16.78) ;

select sign(-998.7878956) ;

select sign(123416.786789) ;

select sign(NULL) ;

select lower('DATABASE') "value" ;

select upper('database') "value" ;

select lower(upper('database')) ;

select upper(lower('database')) ;

SELECT SUBSTRING('This is a test') ;

SELECT SUBSTRING('This is a test', 6) ;

SELECT SUBSTRING('This is a test', 6, 2) "sub value" ;

SELECT SUBSTRING('This is a test', 1, 4) subvalue ;

SELECT SUBSTRING('This is a test', 0, 3) ;

SELECT SUBSTRING('This is a test', 0, 'a') ;

SELECT CHARINDEX('A','DATABASE') ;

SELECT CHARINDEX('A','DATABASE') ;

SELECT CHARINDEX('A','DATABASE', 1) ; --starting position & no occurence

SELECT CHARINDEX('A','DATABASE', 2) ;

SELECT CHARINDEX('A','DATABASE', 3) ;

http://www.sql-server-helper.com/faq/oracle-functions-p01.aspx

http://sqltrim.codeplex.com/SourceControl/latest#trim_test.sql
SELECT LTRIM('00000000012345','0') "value" ; --syntax wrong

SELECT LTRIM('00000000012345') "value" ;

SELECT LTRIM(' 00000000012345') "value" ;

SELECT RTRIM('12345000000000','0') "value" ; --syntax wrong

SELECT RTRIM('12345000') "value" ;

SELECT RTRIM('00000000012345 ') "value" ; --removes trailing spaces only

CREATE TABLE DEPT


( DEPTNO integer,
DNAME VARCHAR(14),
LOC VARCHAR(13)
) ;

Insert into DEPT (DEPTNO,DNAME,LOC) values (10,'ACCOUNTING','NEW YORK');


Insert into DEPT (DEPTNO,DNAME,LOC) values (20,'RESEARCH','DALLAS');
Insert into DEPT (DEPTNO,DNAME,LOC) values (30,'SALES','CHICAGO');
Insert into DEPT (DEPTNO,DNAME,LOC) values (40,'OPERATIONS','BOSTON');

select empno, ename, sal, comm, a.DEPTNO, b.dname


from emp a, DEPT b
where a.DEPTNO = b.DEPTNO;

select a.empno, a.ename, a.sal, a.comm, a.DEPTNO, b.dname


from emp a INNER JOIN DEPT b
ON a.DEPTNO = b.DEPTNO;

select empno, ename, sal, a.DEPTNO, b.dname


from emp a LEFT JOIN DEPT b
ON a.DEPTNO = b.DEPTNO
order by a.DEPTNO;

select empno, ename, sal, a.DEPTNO, b.dname


from emp a LEFT OUTER JOIN DEPT b
ON a.DEPTNO = b.DEPTNO
order by a.DEPTNO;

select a.empno, a.ename, a.sal, a.comm, b.DEPTNO, b.dname


from emp a RIGHT JOIN DEPT b
ON a.DEPTNO = b.DEPTNO;

select a.empno, a.ename, a.sal, a.comm, b.DEPTNO, b.dname


from emp a RIGHT OUTER JOIN DEPT b
ON a.DEPTNO = b.DEPTNO;

select empno, ename, sal, a.DEPTNO, b.DEPTNO, b.dname


from emp a FULL JOIN DEPT b
ON a.DEPTNO = b.DEPTNO;

select a.empno, a.ename, a.sal, a.comm, b.DEPTNO, b.dname


from emp a FULL OUTER JOIN DEPT b
ON a.DEPTNO = b.DEPTNO;

select empno, ename, sal, a.DEPTNO, b.DEPTNO, b.dname


from emp a, DEPT b;

select a.empno, a.ename, a.sal, a.comm, a.DEPTNO, b.dname


from emp a, DEPT b
where a.DEPTNO > b.DEPTNO; ----non equi join

select a.empno, a.ename, b.ename mgrname from emp a, emp b


where a.mgr = b.empno
order by a.empno;

create table tablex (cola integer);


create table tabley (colb integer);

insert into tabley values(1);

select * from tablex a right join tabley b on a.cola = b.colb ;

select count(*) from tablex a, tabley b where a.cola = b.colb;

select count(*) from tablex a LEFT JOIN tabley b ON a.cola = b.colb ;

select count(*) from tablex a RIGHT JOIN tabley b ON a.cola = b.colb;

select count(*) from tablex a full join tabley b on a.cola = b.colb;

select max(sal) from emp;

select max(sal) from emp where sal < (select max(sal) from emp);

select sal from emp a where 4 = (select count(sal) from emp b where a.sal <=
b.sal);

select sal from emp a where 2 = (select count(sal) from emp b where a.sal <=
b.sal);

select sal from emp a where 12 = (select count(sal) from emp b where a.sal <=
b.sal);

select sal from emp a where 3 = (select count(distinct sal) from emp b where a.sal
<= b.sal);

select sal from emp a where 2 = (select count(distinct sal) from emp b where a.sal
<= b.sal);

select sal from emp a where 9 = (select count(distinct sal) from emp b where a.sal
<= b.sal);

select sal from emp a where 9 = (select count(distinct sal) from emp b where a.sal
>= b.sal);

select empno, ename, sal, deptno, rank() over(order by sal) rn from emp;

select empno, ename, sal, deptno, rank() over(order by sal desc) rn from emp;

select empno, ename, sal, deptno, dense_rank() over(order by sal) rn from emp;

select empno, ename, sal, deptno, dense_rank() over(order by sal desc) rn from emp;
select empno, ename, sal, deptno, rank() over(order by sal desc) rn from emp
where rn = 2;

select a.* from (select empno, ename, sal, deptno, rank() over(order by sal desc)
rn from emp) a
where rn = 2;

select b.* from (select empno, ename, sal, deptno, rank() over(order by sal desc)
rn from emp) b
where rn = 2 or rn = 6;

select b.empno, b.ename, b.sal, b.deptno from (select empno, ename, sal, deptno,
rank() over(order by sal desc) rn from emp) b
where rn = 3 or rn = 6;

select a.* from (


select empno, ename, sal, deptno, dense_rank() over(order by sal desc) rn from emp)
a
where rn = 4 or rn = 5;

select a.empno, a.ename, a.sal, a.deptno from (


select empno, ename, sal, deptno, rank() over(order by sal desc) rn from emp) a
where rn = 4 or rn = 5;

select empno, ename, sal, deptno, rank() over(partition by deptno order by sal
desc) rn from emp;

select empno, ename, sal, sum(sal) over(order by sal) cume from emp;

select sum(sal) from emp;

select t.empno, t.ename, t.sal, t.deptno, b.dname


from (select a.empno, a.ename, a.sal, a.deptno from (
select empno, ename, sal, deptno, dense_rank() over(order by sal desc) rn from emp)
a
where rn = 4 or rn = 5) t, dept b
where t.deptno = b.deptno;

select t.empno, t.ename, t.sal, t.deptno, b.dname, t.rn


from (select a.empno, a.ename, a.sal, a.deptno, a.rn from (
select empno, ename, sal, deptno, dense_rank() over(order by sal desc) rn from emp)
a
where rn = 4 or rn = 5) t, dept b
where t.deptno = b.deptno;

select empno, ename, sal, deptno,


case deptno
when 10 then sal*25/100
when 20 then sal*15/100
when 30 then sal*10/100
else sal*5/100
end hike
from emp
order by deptno;

select empno, ename, sal, deptno,


CASE
WHEN deptno = 10 or empno = 7788 THEN sal*20/100
WHEN deptno = 20 and empno = 7902 THEN sal*17/100
WHEN deptno = 30 and ename = 'JAMES' THEN sal*12/100
ELSE sal*4/100
END bonus
from emp
order by deptno;

---NO DECODE---

select GETDATE() ;

select GETDATE()+1 ;

SELECT CONVERT(DATE,GETDATE()+1) AS NextDay

select DATEADD(dd, 1, GETDATE());

select GETDATE()-11 ;

select DATEADD(dd, -1, GETDATE());

select add_months(GETDATE(), 1) ; --error

select DATEADD(mm, 1, GETDATE());

select DATEADD(mm, -3, GETDATE());

select last_day(GETDATE()) ;

SELECT DATEDIFF(m,0,GETDATE()); ---0 means 1900

SELECT DATEADD(mm, DATEDIFF(m,0,GETDATE()),0);

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0));

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0));

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0));

select datediff(mm, GETDATE(), hiredate) months from emp;

select datediff(mm, hiredate, GETDATE()) months from emp;

select DATEPART(day, getdate()) --- value in integer

select DATEPART(year, getdate())

select DATEPART(month, getdate())

select DATENAME(day, getdate()) --- value in string

select DATENAME(year, getdate())

select DATENAME(month, getdate())

select DAY(getdate())

select month(getdate())
select year(getdate())

select year(CURRENT_TIMESTAMP)

select datename(dw,dateadd(day,1,getdate())) ---To know which day

select datename(dw,getdate())

SELECT REPLICATE('_', 5) ;

SELECT REPLICATE('_', 5) + 'abc';

SELECT RIGHT(REPLICATE('_', 5) + 'abc', 5);

SELECT LPAD('abc', 5, '_') FROM dual; --- ORACLE __abc

SELECT LEFT(REPLICATE('_', 2) + 'abc', 5);

SELECT LPAD('abc', 2, '_') FROM dual; --- ORACLE ab

SELECT RIGHT(REPLICATE('_', 2) + LEFT('abc', 2), 2);

SELECT CONCAT('It', '''');

SELECT CONCAT('It', '''', 's');

SELECT CONCAT('It', '''', 's a program');

SELECT 'It' + '''' + 's a program';

select ascii('a') ;

select ascii('A') ;

select ascii('A12345') ;

select char(65) ;

select char(39) ;

select char(97) ; --ascii code

select nchar(97) ; --unicode

SELECT PATINDEX('%world%', 'HelloWorldProgram');

SELECT PATINDEX('%o_r%', 'HelloWorldProgram');

SELECT PATINDEX('%l%gram', 'HelloWorldProgram');

SELECT PATINDEX('%[aeiou]%', 'HelloWorldProgram');

SELECT PATINDEX('%@%','arrigologix@gmail.com')

SELECT PATINDEX('%@%','arrigologix@gmail.com')-1

SELECT SUBSTRING('arrigologix@gmail.com', 1, PATINDEX('%@


%','arrigologix@gmail.com')-1)
select DATALENGTH('arrigologix@gmail.com')

select DATALENGTH('arrigologix@gmail.com') - PATINDEX('%@


%','arrigologix@gmail.com')

SELECT SUBSTRING('arrigologix@gmail.com',
PATINDEX('%@%','arrigologix@gmail.com')+1,
DATALENGTH('arrigologix@gmail.com') -
PATINDEX('%@%','arrigologix@gmail.com')
)

declare @name varchar(30)


set @name = 'arrigologix@gmail.com'

SELECT SUBSTRING(@name,
PATINDEX('%@%',@name)+1,
DATALENGTH(@name) -
PATINDEX('%@%',@name)
)

select 'emp' + SPACE(3) + 'no'

SELECT STUFF('HelloWorldProgram', 1, 10, 'SampleSQL');

SELECT STUFF('HelloWorldProgram', 6, 5, ' ');

SELECT STUFF('HelloWorldProgram', 11, 7, 'Prg Always');

select replace('123123tech','123') name ; --error

select replace('123123tech','123','') name ;


select replace('123123tech','123','0') name ;

select replace('database','a','@') name ;

select replace('1234@#*','@#*','s') name ;

select replace('1234@#*','#*','c') name ;

select replace('1234@#*','@*','c') name ;

select replace('database','a','') name ;

select len(replace('database','a','')) name ;

select len('database') - len(replace('database','a','')) name ;

select ceiling(3456.7890) ;

select ceiling(56.001) ;

select ceiling(-98.001) ;

select floor(3456.7890) ;

select floor(56.001) ;

select floor(-98.001) ;
select round(4583478.12381, 1) ;

select round(4583478.512381, 1) ;

select round(4583478.512381, 0) ;

select round(4583478.512381, 2) ;

select round(4583478.512381, 2, 1) ;

select round(4583478.512381, 4) ;

select round(4583478.512381, 4, 2) ;

select round(4583478.512381, 4, 1) ;

select round(4583478.512381, -2) ;

select round(4583478.512381, -4) ;

select round(4583478.512381, -3) ;

SELECT ROUND(125.315, 1, 1);

DECLARE @Value DECIMAL(28, 6)

SET @Value = 26.153875

SELECT @Value AS OriginalValue,


ROUND(@Value, 4, 0) AS Rounding,
ROUND(@Value, 4, 1) AS Truncating

select sal, comm, sal*comm from emp;

select sal, comm, sal*isnull(comm,0) from emp;

select sal, comm, sal*isnull(comm,1) from emp;

select count(*) from emp;

select count(1) from emp;

select count(7) from emp;

select count(comm) from emp;

select * into emp_311 from emp where 1 = 2

select * into emp_211 from emp;

select empno, ename, sal, comm into emp_411 from emp;

select empno, ename, sal, comm, deptno into emp_611


from emp where deptno = 10 or deptno = 30;

select * into tempEMP from EMP

truncate table tempEMP


select * from tempEMP

insert into tempEMP select * from EMP

create view emp_view111 as (


select empno, ename, sal from emp
);

create view emp_view122 as (


select empno, ename, sal from emp where deptno = 20
);

create view emp_view211 as (


select empno, ename, sal, a.deptno, dname from emp a, dept b
where a.deptno = b.deptno
);

CREATE TABLE dbo.Employee


(id INT IDENTITY(1,1),
FName VARCHAR(50),
LName VARCHAR(50))
GO

CREATE VIEW dbo.vw_Employee


AS
SELECT * FROM dbo.Employee

ALTER TABLE dbo.Employee


ADD StreetAddress VARCHAR(100)

EXEC sp_refreshview 'dbo.vw_Employee'

update emp_view111
set sal = 10000;

select * from EMP;

update emp_view2
set sal = 10000;

drop table cust_nn1;

create table cust_nn1 (

ID integer constraint id_nn1 NOT NULL,


NAME varchar(50),
SAL decimal(11,2),
LOCATION varchar(10),
ADDRESS varchar(20)
);

select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'cust_nn1';

alter table cust_nn1 alter column sal decimal(11,2) NOT NULL;

drop table cust_d1;

create table cust_d1 (


ID integer,
NAME varchar(50),
SAL decimal(11,2),
LOCATION varchar(10),
ADDRESS varchar(20)
);

insert into cust_d1 values (1, 'AAAAA', 12000, 'BNG', 'KARNATAKA');

alter table cust_d1


add constraint DF_sal
default 25000 for sal;

insert into cust_d1 values (2, 'BBBBB', 0, 'CHN', 'TAMILNADU');

insert into cust_d1 values (3, 'CCC', 'HYD', 'TELANgANA');

insert into cust_d1 (ID, NAME, LOCATION, ADDRESS) values (3, 'CCC', 'HYD',
'TELANgANA');

insert into cust_d1 (ID, NAME, LOCATION) values (6, 'EEE', 'DEL');

insert into cust_d1 (ID, NAME) values (8, 'QWERTY');

insert into cust_d1 (ID, NAME, LOCATION, ADDRESS) values (9, 'CCC', 'HYD',
'TELANgANA');

insert into cust_d1 values (10, 'CCC', 25000, 'HYD', 'TELANgANA');

select * from cust_d1 where sal = 25000;

select * from cust_d1 where sal <> 25000;

select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'cust_d1';

select * from sys.default_constraints;

alter table cust_d1 drop constraint DF_sal;

alter table cust_d1 add default 0 for sal;

drop table cust_u1;

create table cust_u1 (

ID integer,
NAME varchar(50),
SAL decimal(11,2),
LOCATION varchar(10),
ADDRESS varchar(20)
);

alter table cust_u1 add constraint us unique(id, sal);

insert into cust_u1 values (1, 'AAAAA', 12000, 'BNG', 'KARNATAKA');

insert into cust_u1 values (1, 'AAAAA', 12000, 'CHN', 'TAMILNADU');


insert into cust_u1 values (2, 'BBBBB', 12000, 'BNG', 'KARNATAKA');

select * from sys.key_constraints;

alter table cust_u1 drop constraint us;

alter table cust_u1 add unique(id, sal);

drop table cust_p1;

create table cust_p1 (

ID integer,
NAME varchar(50),
SAL decimal(11,2),
LOCATION varchar(10),
AGE integer
);

alter table cust_p1 add constraint cust_p1_id primary key (id);

alter table cust_p1 alter column id integer NOT NULL;

alter table cust_p1 add constraint cust_p1_age primary key (age);

select * from sys.key_constraints;

alter table cust_p1 drop constraint cust_p1_id;

alter table cust_p1 add constraint cust_p1_id_age primary key (id, age);

alter table cust_p1 alter column age integer NOT NULL;

select * from sys.objects;

DROP TABLE CUST_p1;

create table cust_c1 (

ID integer,
NAME varchar(50),
SAL decimal(11,2),
LOCATION varchar(10),
AGE integer
);

insert into cust_c1 values (2, 'BBBBB', 12000, 'BNG', 66);

alter table cust_c1 add constraint cust_c1_age check (age>=18);

insert into cust_C1 values (1, 'AAAAA', 12000, 'BNG', 14);

insert into cust_C1 values (1, 'AAAAA', 12000, 'BNG', 24);

select * from sys.check_constraints;

select * from sys.foreign_keys;

select * from sys.foreign_key_columns;


select a.empno, a.ename, a.sal sal, b.ename mgr_name, b.sal mgr_sal from emp a, emp
b
where a.mgr = b.empno;

select * from (
select a.empno, a.ename, a.sal sal, b.ename mgr_name, b.sal mgr_sal from emp a, emp
b
where a.mgr = b.empno) t
where t.sal > t.mgr_sal;

select empno, count(*) from emp group by empno having count(*) > 1;

select distinct empno from emp;

select distinct sal from emp;

select distinct job, mgr from emp;

select * from emp where deptno = 10 or deptno = 20;

select * from emp where deptno in (10, 20);

select * from emp where deptno = 20 and mgr = 7566;

select * from emp where deptno = 20 and deptno = 30;

select * from emp where sal between 1000 and 4500;

select * from emp where sal >= 1000 and sal <= 4500;

select * from emp where sal >= 1000 or sal <= 4500;

select * from emp where job like 'C%';

select * from emp where job like '_L%';

select datename(dw, getdate()+1)

select datename(dw, getdate())

select ISNUMERIC(123)

select ISNUMERIC('123')

select ISNUMERIC('123a')

select isdate(getdate())

select isdate('123')

SELECT ISDATE('2015-05-01')

SELECT ISDATE('2015-05-01 10:03')

SELECT ISDATE('2015-05-01 10:03:32')

SELECT ISDATE('2015-05-01 10:03:32.001')


select nullif(12, 12)

select nullif(23, 12)

select nullif(NULL, 12)

select id, name from tab1


except
select id, name from tabb;

select id, cast(name as char(10)) name from tab1


except
select id, name from tabb;

SELECT CAST(14.85 AS int)

SELECT CAST(14.85 AS float)

SELECT CAST(15.6 AS varchar)

SELECT CAST(15.6 AS varchar(4))

SELECT CAST('15.6' AS float)

SELECT CAST('2014-05-02' AS datetime)

SELECT CONVERT(int, 14.85)

SELECT CONVERT(float, 14.85)

SELECT CONVERT(varchar, 15.6)

SELECT CONVERT(varchar(4), 15.6)

SELECT CONVERT(float, '15.6')

SELECT CONVERT(datetime, '2014-05-02')

SELECT CONVERT(varchar, '05/02/2014', 101)

SELECT CONVERT(date, getdate(), 0)

CREATE TABLE dbo.CustomerAddress


(
FName VARCHAR(100),
LName VARCHAR(100),
HouseNumber INTEGER,
StreetName VARCHAR(100),
City VARCHAR(100),
State CHAR(2)
)

go

INSERT INTO dbo.CustomerAddress


VALUES ('Hari', 'Krishna', 123, 'Test Street', 'Charlotte', 'NC')
go

INSERT INTO dbo.CustomerAddress


VALUES ('Sri', 'Rama', NULL, 'ELM Street', 'Charlotte', 'NC')

go

SELECT *,
HouseNumber + ' '
+ StreetName + ' ' + City + ' ' + State AS FullStreetAddress
FROM dbo.CustomerAddress

--USE the Plus Sign to Concatenate String Values in SQL Server


SELECT *,
Cast(HouseNumber AS VARCHAR(10)) + ' '
+ StreetName + ' ' + City + ' ' + State AS FullStreetAddress
FROM dbo.CustomerAddress

--Use ISNULL To Handle Null Contatenation in SQL Serve when using Plus Sign
SELECT *,
Isnull(Cast(HouseNumber AS VARCHAR(10)), '')
+ ' ' + StreetName + ' ' + City + ' ' + State AS FullStreetAddress
FROM dbo.CustomerAddress

--USE the CONCAT Function to Concatenate String Values in SQL Server


SELECT *,
Concat(Cast(HouseNumber AS VARCHAR(10)), ' ', StreetName, ' ', City, ' ',
State)

AS FullStreetAddress
FROM dbo.CustomerAddress

Select *,Cast(HouseNumber AS VARCHAR(10)) +' '+StreetName+' '+City+' '+State

AS FullAddress
from dbo.CustomerAddress

dbcc useroptions

SET concat_null_yields_null OFF

Select *,Cast(HouseNumber AS VARCHAR(10)) +' '+StreetName+' '+City+'


'+ISNULL(State,'')

AS FullAddress,
ISNULL(State,'Unknow') AS StateAvailableOrNot
from dbo.CustomerAddress

Select *,Cast(HouseNumber AS VARCHAR(10)) +' '+StreetName+' '+City+'


'+ISNULL(State,'')

AS FullAddress,
Case
When State is null Then 'Unknown' ELSE State END AS StateAvailableOrNot
from dbo.CustomerAddress

SELECT TOP(5) *
FROM emp
WHERE sal <= 4000;
SELECT TOP(25) PERCENT *
FROM emp
WHERE sal <= 4000;

DELETE TOP(5)
FROM emp
WHERE sal <= 4000;

DELETE TOP(25) PERCENT


FROM emp
WHERE sal <= 4000;

select t.name as TableName, SCHEMA_NAME(t.SCHEMA_ID), rows


from sysindexes i ,
sys.tables t

where i.id = t.object_id


and first IS NOT NULL

select SCHEMA_NAME(schema_id), name from sys.tables

select SCHEMA_NAME(schema_id)+'.'+name from sys.tables

select 'TRUNCATE TABLE ' + SCHEMA_NAME(schema_id)+'.'+name from sys.tables

Das könnte Ihnen auch gefallen