열공~ 정원혁 교재(SQL 2000) 베끼기 그리고 요약(1)
열공~ 정원혁 교재(SQL 2000) 베끼기 그리고 요약(1)
Name 강사
use pubs; /* DB 사용 */
exec sp_help titles; /* 프로시저 사용하여 일부 컬럼을 획득 */
exec sp_columns titles; /* 위의 구문과 동일한 효과 */
select title_id, title, price, pub_id from titles; /* 검색 조회 */
select title_id, pub_id, price, title from titles;
select '책 번호:', title_id, pub_id, price, title from titles; /* 가상 컬럼을 이용하여 결과 보이기 */
select title_id as title_no, pub_id as [출판사 번호], price, title from titles; /* 가상 컬럼(출판사 번호) 사용 */
select * from publishers
sp_help publishers; /* publishers 테이블에 어떠한 컬럼이 있는지 확인 */
sp_columns publishers; /* 위 구문과 동일한 효과 */
declare @id INT, @name char(10)
set @id = 0
set @name = '홍길동'
select @id, @name
/* 변수의 선언과 값의 설정 */
sp_addtype tProdID , 'char(6)'
--1)
select title_id as title_no
, pub_id as '출판사 번호'
, price
, convert(char(30), title) as ' 줄인 제목'
from titles;
--2)
select title_id as title_no
, pub_id as '출판사 번호'
, price
, substring(title,1,30) as ' 줄인 제목'
from titles;
select title_id as title_no
, pub_id as '출판사 번호'
, price *1.1 as '가격(부가세 포함)'
, substring(title,1,30) as '줄인 제목'
from titles;
select title_id as title_no
, pub_id as '출판사 번호'
, price *1.1 as '가격(부가세 포함)'
, cast(title as char(30)) as '줄인 제목'
from titles;
select floor(29.9), floor(29.1)
select ceiling(29.9), ceiling(29.1)
select round (29.1, 0), round (29.9, 0), round(29.953, 1)
------------------------------------------------------------
select title_id, price, floor(price) as floor, ceiling(price) as ceiling, round(price, 0) as round from titles;
/* 각종 산술적 연산 함수를 사용하여 검색에 적용한 사례 p.85 */
/* 문자에 대한 함수 교과서 반드시 참조 */
-------- 아래는 주의할 사항 --------
select substring('한글abcd데이타',1,2) --1)
select substring('한글abcd데이터',3,3) --2)
/* 주의사항 : substring, left, right, len 등은 한글과 영문에 대해서 자리수 계산을 다르게 한다. */
select soundex('한글'); /* 한글에 대해서는 무용지물 !!! */
select difference('smithers','smothers');
select difference('한글','한글');
select difference('한글','한글의'); /* 역시 문제가 있음 ! */
select difference('한글','희망'); /* 전혀 안 먹힘 ! */
select difference('한글','가나다'); /* 오홋~~~~ 이럴수가~~~*/
select replicate('반복',10); -- 정말 10번 나옴. 이것은 먹힙니다.
select reverse('오my갓'); --으흠 잘 되는구만.....
select stuff('123456',3,2,'abcde'); --유용함!
select quotename('abc[]def'); -- 정원혁 본좌님께서 몰라도 된다고 하심~~
-- 이것도 몰라도 된다고 하심
-- unicode
declare @nStr nchar(12)
set @nStr = ' 한글'
select unicode(@nStr), nchar(unicode(@nStr))
select getdate()
-- 밀리세컨드까지 데이터를 가져 오고자 한다면....
select convert(varchar(30), getdate(),9)
- 우리에게 친숙한 yy.mm.dd 로 표현하고자 한다면...
select convert(varchar(30), getdate(), 102)
select datepart(mm, getdate())
select month(getdate()) -- 원하는 "월"만 가져오려면?
select dateadd(mm, 20, getdate()) -- 오늘로 부터 30개월 후는 ?
select dateadd(dd, 100, getdate()) -- 오늘로 부터 100일후는?
select getdate() + 100 -- 위의 결과와 동일
select datediff(dd, getdate(), '3000.1.1') -- 지금부터 3000년까지는 몇일이 남았을까?
select datename(dw, getdate()), datename(mm, getdate()) -- 한글 요일명이 출력됨!
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- 요기까지가 교과서 92페이지까지입니다. lsh 샘 올림
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
select app_name()
select title_id, qty from sales; -- 책번호와 판매수량을 가지고 오는 질의어
select title_id, qty from sales where qty >= 20
-- 수량이 20 이상인 것만 검색
select title_id, qty from sales where title_id = 'BU1032'
-- title_id 가 BU1032 검색
-- =, >, <, >=, <=, <> , !=. !>, !< 이런 것들이 비교 연산자들입니다.
select title_id, price from titles where price is null
-- 가격이 없는 것을 검색
--- 다음의 차이를 알아보자 !
select title_id, price from titles where price = null -- 결과가 안 나옵니다....그러나 이후에 보면....
select title_id, price from titles where price <> null -- 요것도 그렇습니다.
select title_id, price from titles where price is not null -- 오홋~ 이것은 나옵니다. 그렇죠.
---------------------
------- 참고 사항 -----
---------------------
-- ANSI 설정에서는 NULL 값에 대해서 비교할 때는 =를 사용해서는 안된다. 반드시 IS NULL 또는 IS NOT NULL을 사용해야 한다.
-- 이것은 ANSI 규정을 준수하기 위한 것이다.
-- 그러나 이 구문을 주게 되면 얘기가 달라집니다.
set ANSI_NULLS OFF
select title_id, price from titles where price = null -- 요럴수가!!! 결과가 지대로 나옵니다.
select title_id, price from titles where price <> null -- 요것도 그렇습니다.
set ansi_NULLS on
-- 그러나 다시 리셋하면....
select title_id, price from titles where price = null -- 도로시 백!
________________________________________________________________
________________________________________________________________
----- 다음 부터는 "정열"적인 "정렬"의 얘기입니다. 교과서 p.96 부터죠.
________________________________________________________________
________________________________________________________________
select title_id, qty from sales order by qty
select title_id, qty from sales order by qty DESC
________________________________________________________________
------ 아래의 두 구문의 차이를 문여겨 보자!
________________________________________________________________
select title_id, qty from sales order by title_id, qty DESC
-- title_id는 오름차순, qty는 내림차순이다.
-- 그럼 둘다 내림차순이 되려면.....
select title_id, qty from sales order by title_id DESC, qty DESC
select title_id, qty from sales order by 1 DESC, 2 DESC
--- 98페이지까증~ ---
[이 게시물은 486i님에 의해 2008-04-17 15:59:19 예제노트 활용도100%에서 이동 됨]
[이 게시물은 486i님에 의해 2008-04-21 15:10:08 MS-SQL에서 이동 됨]