Below is the oracle SQL query currently used in the existing system, I am trying to convert this SQL into a SQL Server query for the new system.
I have been trying different format, but I am not successful and also since I am experienced in Oracle than SQL Server.
Kindly help me in this regard.
SELECT DISTINCT b.CONTRACT_KEY, b.STARTDATE, b.ENDDATE, CASE WHEN MULTI_YEAR_FLAG = 'N' THEN null WHEN TRUNC(b.ENDDATE, 'Y') <= TRUNC(SYSDATE, 'Y') THEN null WHEN TRUNC(TO_DATE(EXTRACT (YEAR FROM SYSDATE) || '-' || EXTRACT (MONTH FROM b.STARTDATE) || '-' || EXTRACT (DAY FROM b.STARTDATE_UPDATED), 'YYYY-MM-DD'),'Q') >= TRUNC(SYSDATE, 'Q') THEN TO_DATE(EXTRACT (YEAR FROM SYSDATE) || '-' || EXTRACT (MONTH FROM b.STARTDATE) || '-' || EXTRACT (DAY FROM b.STARTDATE_UPDATED), 'YYYY-MM-DD') WHEN TRUNC(TO_DATE(EXTRACT (YEAR FROM ADD_MONTHS(SYSDATE,12)) || '-' || EXTRACT (MONTH FROM b.STARTDATE) || '-' || EXTRACT (DAY FROM b.STARTDATE_UPDATED), 'YYYY-MM-DD'),'Y') < TRUNC(b.ENDDATE, 'Y') THEN TO_DATE(EXTRACT (YEAR FROM ADD_MONTHS(SYSDATE,12)) || '-' || EXTRACT (MONTH FROM b.STARTDATE) || '-' || EXTRACT (DAY FROM b.STARTDATE_UPDATED), 'YYYY-MM-DD') END "NEXT_MYA_DATE" FROM SALES_ENT.CONTRACT_N_DIM_BT b
Advertisement
Answer
Based Martin’s suggestion, i worked through and made the sql to work finally. Thanks again.
SELECT DISTINCT b.contract_key, b.startdate, b.enddate, CASE --Non Multi-Year WHEN multi_year_flag = 'N' THEN NULL --Contracts expiring in current year or before WHEN CAST(datepart(year, b.enddate) AS INT) <= CAST(datepart(year, getdate()) AS INT) THEN NULL --Contracts with anniversary in current year WHEN datepart(quarter, b.startdate) >= datepart(quarter, getdate()) --cast(datepart(YEAR, getdate() as int) + '-' + cast(datepart(MONTH, b.STARTDATE) as INT) + '-' + cast(datepart(DAY, b.STARTDATE_UPDATED) as INT), 'YYYY-MM-DD'),'Q') >= TRUNC(SYSDATE, 'Q') THEN datepart(year, getdate()) + '-' + datepart(month, b.startdate) + '-' + datepart(day, b.startdate_updated) --Contracts with anniversary next year WHEN datepart(year, dateadd(month, 12, getdate())) < datepart(year, b.enddate) THEN datepart(year, dateadd(month, 12, getdate())) + '-' + datepart(month, b.startdate) + '-' + datepart(day, b.startdate_updated ) END "NEXT_MYA_DATE", FROM sales_ent.contract_n_dim_bt b