|
차이점(MS-SQL 사용시 유의점)
1. MS-SQL2000은 재귀순환쿼리 (ORACLE 의 CONNECT BY) 구문이 없습니다..
MS-SQL2005에서는 지원된다고 합니다.
따라서 재귀순환이 필요할 경우 함수를 만들어 사용해야 합니다.
ex)
-------------------------------------------------------------------
CREATE FUNCTION fn_recursivemenu(@current as varchar(10))
RETURNS @Hierarchy table (
item_idx [int] IDENTITY (1, 1) NOT NULL , item varchar(20), up_item varchar(20), lvl int
)
-- Recusrsive메뉴 구현
as
begin
DECLARE @lvl int
DECLARE @stack table (item varchar(20), up_item varchar(20), lvl int)
INSERT INTO @stack VALUES (@current, null, 1)
SELECT @lvl = 1
WHILE @lvl > 0
BEGIN
IF EXISTS (SELECT * FROM @stack WHERE lvl = @lvl)
BEGIN
SELECT @current = item
FROM @stack
WHERE lvl = @lvl
INSERT INTO @Hierarchy
SELECT menu_id, up_menu_id, menu_level FROM SYS_MENU
WHERE menu_id = @current
DELETE FROM @stack
WHERE lvl = @lvl
AND item = @current
INSERT @stack
SELECT menu_id, up_menu_id, @lvl + 1
FROM SYS_MENU
WHERE up_menu_id = @current
ORDER BY menu_id desc
IF @@ROWCOUNT > 0
SELECT @lvl = @lvl + 1
END
ELSE
SELECT @lvl = @lvl - 1
END
RETURN
END
------------------------------------------------------------
2. MS-SQL 은 하위쿼리 테이블 쿼리시 꼭 alias 명을 명시적으로
부여해야 됩니다.
ex) select * from (select * from table) --- 에러
select * from (select * from table) a --- OK
3. MS-SQL2000 Rownum 매기는법(편법)
: MS-SQL에서 순차적인 번호를 매기기 위해서는 IDENTITY를 컬럼에 부여해주는
방법밖에 없습니다.
꼭 순차적인 번호를 부여해야 하는 경우라면 아래예문같은 방법을 사용해서 구현
할 수는 있습니다만 그다지 추천할 방법은 아닙니다.
ex)
-------------------------
SELECT
((SELECT count(*) FROM [테이블명] b where b.키컬럼 <= a.키컬럼)+200700000) AS ROWNUM
FROM
[테이블명] a
ORDER BY 키컬럼
-------------------------
[결과]
200700001
200700002
Math Functions | ||
---|---|---|
Function | Oracle | SQL Server |
Absolute value | ABS | ABS |
Arc cosine | ACOS | ACOS |
Arc sine | ASIN | ASIN |
Arc tangent of n | ATAN | ATAN |
Arc tangent of n and m | ATAN2 | ATN2 |
Smallest integer >= value | CEIL | CEILING |
Cosine | COS | COS |
Hyperbolic cosine | COSH | COT |
Exponential value | EXP | EXP |
Round down to nearest integer | FLOOR | FLOOR |
Natural logarithm | LN | LOG |
Logarithm, any base | LOG(N) | N/A |
Logarithm, base 10 | LOG(10) | LOG10 |
Modulus (remainder) | MOD | USE MODULO (%) OPERATOR |
Power | POWER | POWER |
Random number | N/A | RAND |
Round | ROUND | ROUND |
Sign of number | SIGN | SIGN |
Sine | SIN | SIN |
Hyperbolic sine | SINH | N/A |
Square root | SQRT | SQRT |
Tangent | TAN | TAN |
Hyperbolic tangent | TANH | N/A |
Truncate | TRUNC | N/A |
Highest number in list | GREATEST | N/A |
Lowest number in list | LEAST | N/A |
Convert number if NULL | NVL | ISNULL |
String Functions | ||
---|---|---|
Function | Oracle | SQL Server |
Convert character to ASCII | ASCII | ASCII |
String concatenate | CONCAT | (expression + expression) |
Convert ASCII to character | CHR | CHAR |
Return starting point of character in character string (from left) | INSTR | CHARINDEX |
Convert characters to lowercase | LOWER | LOWER |
Convert characters to uppercase | UPPER | UPPER |
Pad left side of character string | LPAD | N/A |
Remove leading blank spaces | LTRIM | LTRIM |
Remove trailing blank spaces | RTRIM | RTRIM |
Starting point of pattern in character string | INSTR | PATINDEX |
Repeat character string multiple times | RPAD | REPLICATE |
Phonetic representation of character string | SOUNDEX | SOUNDEX |
String of repeated spaces | RPAD | SPACE |
Character data converted from numeric data | TO_CHAR | STR |
Substring | SUBSTR | SUBSTRING |
Replace characters | REPLACE | STUFF |
Capitalize first letter of each word in string | INITCAP | N/A |
Translate character string | TRANSLATE | N/A |
Length of character string | LENGTH | DATALENGTH or LEN |
Greatest character string in list | GREATEST | N/A |
Least character string in list | LEAST | N/A |
Convert string if NULL | NVL | ISNULL |
Date Functions | ||
---|---|---|
Function | Oracle | SQL Server |
Date addition | (use +) | DATEADD |
Date subtraction | (use -) | DATEDIFF |
Last day of month | LAST_DAY | N/A |
Time zone conversion | NEW_TIME | N/A |
First weekday after date | NEXT_DAY | N/A |
Convert date to string | TO_CHAR | DATENAME |
Convert date to number | TO_NUMBER(TO_CHAR()) | DATEPART |
Convert string to date | TO_DATE | CAST |
Get current date and time | SYSDATE | GETDATE() |
substr == substring
to_char == convert
--to_char(to_date(a.accept_da,'YYYY-MM-DD'), 'yyyy.mm.dd'),
convert(varchar(10),cast(a.accept_da as smalldatetime),102),
-- to_char(a.dr_amt, '999,999,999,999,999'),
subString(convert(varchar, cast(a.dr_amt as money ),1),1,len(convert(varchar, cast(a.dr_amt as money ),1))-3),
subString(convert(varchar, cast(a.cr_amt as money ),1),1,len(convert(varchar, cast(a.cr_amt as money ),1))-3),
--TO_CHAR(last_day(DATEADD(month,substring('20050127',1,6) + '01',-1)), 'YYYYMMDD')
dateadd(dd, -datepart(dd, cast('20050127' as smalldatetime)), dateadd(mm,1,cast('20050127' as smalldatetime)))
LPAD == replace
NVL == COALESCE
-- AND ROWNUM = 1
TOP 1
-- DECODE(dr_cr, 'D', SUM(currency_amt), 0) debt_amount,
-- DECODE(dr_cr, 'C', SUM(currency_amt), 0) credit_amount,
(case dr_cr when 'D' then SUM(currency_amt) else 0 end) debt_amount,
(case dr_cr when 'C' then SUM(currency_amt) else 0 end) credit_amount,
--select LPAD(' ',TO_NUMBER(s.accnt_gubun) * 4,' ') || s.accnt_detal_name from tb_erp_faccnt_cd s where s.accnt_cd = a.accnt_cd) accnt_detal_name ,
select accnt_cd, (select right(space(cast(s.accnt_gubun as int) * 4)+s.accnt_gubun,cast(s.accnt_gubun as int) * 4)+ s.accnt_detal_name from tb_erp_faccnt_cd s where s.accnt_cd = a.accnt_cd) accnt_detal_name ,
--to_char(add_months(to_date('@@accept_da_fr','yyyymmdd'),-12),'yyyymmdd')
convert(varchar(8),dateadd(month,-12,cast('@@accept_da_fr' as datetime)),112)
오라클의 trunc 함수를 MS-SQL에서 구현하기...
MSSQL
select
cast(round(A,0,1) as decimal(10))
from [TABLE]
# ORACLE
select trunc(A) from [TABLE]
제목 : 오라클의 trunc와 같은 기능을 하는것??
create table AAA
(
A float
)
insert AAA values (10.12345)
insert AAA values (10.445)
select round(A,2,1), cast(round(A,2,1) as decimal(10,2)) from AAA
10.119999999999999 10.12
10.44 10.44
to_date --> cast
오라클에서 to_date는 형을 데이터 형식으로 바꾸는 함수 인데
to_date(컬럼명, 'YYYYMMDD') 이렇게 써주어서 변환했는데 MS-SQL에서는 CAST를 쓴다.
CAST(컬럼명 as smalldatetime) 이렇게 데이터 타입으로 바꾼다.
그럼 CONVERT는 안될까? ㅡㅡ;;
방금해봤는데..되긴된다.. 쿠허허허허
CONVERT(smalldatetime, 컬럼명)
출처 : http://blog.naver.com/pis_guy?Redirect=Log&logNo=120035477379
'DB' 카테고리의 다른 글
where 에서 like 대신 INSTR 사용 하는 것도 (0) | 2008.11.04 |
---|---|
한방에 Select 하여 Update하기 (0) | 2008.05.21 |
[SQL]중복 값 갯수 구하기 (0) | 2007.05.23 |
Database Programming & Design (0) | 2007.05.19 |
Database Programming & Design (0) | 2007.05.18 |