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
COLLATEclause - Always specify column names in
INSERTstatements - Move
ORDER BYclauses out of subqueries - Get rid of
REPLACE(expression, "'", "''")(and parameterize the expression) - Replace
INSTRwithCHARINDEXbut be aware of the differences - Change
SUBSTR(expression, start, length)toSUBSTRING(expression, start, length)but watch for missing arguments or negative numbers - Replace
SYSDATEwithGETDATE() - When
ROUNDis used with dates, change toDATEDIFF - Look for
ROUND(expression)with only one argument; change toROUND(expression, 0) - Change
DECODEtoIF-ELSEstatements - Change
TO_DATE(expression)toCONVERT(date, expression)(or maybe just remove altogether if expression is already a date) - Global variable
OracleDatecan usually be replaced withGETDATE(), but watch for text formatting - Look for
TRUNCand 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
ROWNUMtoROW_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
||toCONCATfor string concatenation. (CONCAT(exp1, exp2)is preferred overexp1 + exp2because 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
LENGTHtoLEN - Don't
INSERT ''when you really want toINSERT NULL - Replace
NULLS FIRSTconstruct inORDER BYclause - Replace
sequence.NEXTVALwithNEXT VALUE FOR Sequence