I am migrating my oracle database to SQL Server. I'm in need of some help converting this one line of code in my WHERE clause
TO_DATE(TBL_TMX_ATTEMPT.LSTUPD) > (SYSDATE - '5') You can do:
WHERE CONVERT(DATETIME,TBL_TMX_ATTEMPT.LSTUPD) > GETDATE()-5
If LSTUPD is already in a datetime, then omit the CONVERT(). No need to run the conversion if it is already IN the right format.
Also keep in mind GETDATE() includes a time stamp. So this is the current date/time - 5 days.
If you want to get 5 days before midnight use this:
WHERE CONVERT(DATETIME,TBL_TMX_ATTEMPT.LSTUPD) > CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120))-5
It's important to know what the data type of TBL_TMX_ATTEMPT.LSTUPD is. If it is a VARCHAR2 or other string type (BAD choice for storing dates, btw), you need to take the date formats into consideration when calling CONVERT in SQL Server. Look up the Date/Time Style parameter of the CONVERT function for more info.
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
SYSDATE - '5'is not a good style in the first place.'5'is a character literal, not a number. You should always use the correct literals.5is a number. Btw: Why are you storing a date in avarcharcolumn? If you are migrating you should also take the chance to use the correct datatype and store that in adateordatetimecolumn. Storing a date in a character column is a really bad choice.LSTUPD, I can't help but think it's supposed to read, "L-stupid."date - 5is well defined and documented in Oracle (it's days). It's the same in other DBMS (Postgres, Firebird, H2 also work that way). But I agree: using a standard intervaldate - interval '5' dayis probably clearer and more portable.