Things to look for when rewriting Oracle SQL statements for use in SQL Server:
- Parameterize all the things!
- Fix unnamed subqueries (or consider converting to a join)
- If a filter should be case-sensitive but isn't, add a
COLLATE
clause - Always specify column names in
INSERT
statements - Move
ORDER BY
clauses out of subqueries - Get rid of
REPLACE(expression, "'", "''")
(and parameterize the expression) - Replace
INSTR
withCHARINDEX
but be aware of the differences - Change
SUBSTR(expression, start, length)
toSUBSTRING(expression, start, length)
but watch for missing arguments or negative numbers - Replace
SYSDATE
withGETDATE()
- When
ROUND
is used with dates, change toDATEDIFF
- Look for
ROUND(expression)
with only one argument; change toROUND(expression, 0)
- Change
DECODE
toIF-ELSE
statements - Change
TO_DATE(expression)
toCONVERT(date, expression)
(or maybe just remove altogether if expression is already a date) - Global variable
OracleDate
can usually be replaced withGETDATE()
, but watch for text formatting - Look for
TRUNC
and fix it somehow - Change
TO_NUMBER(expression)
toCONVERT(int, expression)
(ordecimal
, etc.) (or maybe just remove altogether if conversion is unnecessary) - Change
TO_CHAR(expression, format)
to something else, maybe justexpression
? - Convert
ROWNUM
toROW_NUMBER() OVER ([PARTITION BY value_expression] ORDER BY order_by_expression)
- Change
ADD_MONTHS(date, integer)
toDATEADD(datepart, number, date)
- Remove
FROM DUAL
- Watch for
LOWER(expression)
andUPPER(expression)
and consider that SQL Server is case-insensitive - Change
||
toCONCAT
for string concatenation. (CONCAT(exp1, exp2)
is preferred overexp1 + exp2
because it automatically coerces each expression to a string. Using+
can lead to errors if the first expression is numeric.) - Replace Oracle
(+)
notation for outer joins with ANSI join syntax - Change
LENGTH
toLEN
- Don't
INSERT ''
when you really want toINSERT NULL
- Replace
NULLS FIRST
construct inORDER BY
clause - Replace
sequence.NEXTVAL
withNEXT VALUE FOR Sequence