3

Need SQL query for this month data and previous month last 5 days data together:

SELECT
  CONVERT(VARCHAR (10), wDate, 103) AS wDate,
  Empid,
  Process,
  Model,
  Qty,
  Section,
  Avlbl_Mins,
  NP_Mins,
  L_Mins,
  NP_Reason       AS NPReason,
  Process_Remarks AS PRem,
  Day_Remarks     AS DRem,
  Othermin,
  StdMin,
  Tstdmin,
  TAvlblmin
FROM tblProductionEffcyDetails
WHERE (DAY(EnteredDate) >= DAY(GETDATE()) - 5)
ORDER BY EnteredDate DESC
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40

2 Answers2

1

For SQL SERVER 2012+ use this

WHERE  EnteredDate >=  dateadd(dd,-4,eomonth(getdate(),-1)) 
  and  EnteredDate < dateadd(dd,1,eomonth(getdate()))

For older versions

WHERE  EnteredDate >=  dateadd(dd,-5,DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) 
  and EnteredDate < DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, getdate()) + 1, 0))
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
1

Try this : to get data from last months last five days

WHERE EnteredDate > (DATEADD(DAY,-5,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40