Hi Experts
I am trying to get the first day of the month if any date passed e.g '9/26/2017' the query should give the date first date of the month 9/1/2017 i am unable to figure out the date coming in the result is 8/1/2017
SELECT otr.ITEM_NAME,
otr.START_DATETIME Date1,
DATEADD(mm,DATEDIFF(mm,0,otr.START_DATETIME),0) Firstday, --- First day of the month,
--DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,otr.START_DATETIME)+1,0)) Lastday, ---last day of the month,
otr.PROD_OIL_VOL prod_oil,
otr.ITEM_ID,
otr.ITEM_TYPE
FROM tableotr
WHERE otr.ITEM_NAME IN ('SM')
AND otr.ITEM_TYPE='type_1' AND otr.START_DATETIME >= DATEADD(mm,DATEDIFF(mm,0,otr.START_DATETIME),0) AND otr.START_DATETIME <= '9/26/2017'
AND otr.PROD_OIL_VOL=
(SELECT MAX(PROD_OIL_VOL) FROM table_test
WHERE ITEM_NAME IN ('SM') AND ITEM_TYPE='type_1'
AND otr.ITEM_ID = ITEM_ID
AND START_DATETIME >= DATEADD(mm,DATEDIFF(mm,0,START_DATETIME),0)AND START_DATETIME <= '9/26/2017' )
AND ITEM_TYPE='type_1'
SELECT otr.ITEM_NAME,
otr.START_DATETIME Date1,
DATEADD(mm,DATEDIFF(mm,0,otr.START_DATETIME),0) Firstday, --- First day of the month,
--DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,otr.START_DATETIME)+1,0)) Lastday, ---last day of the month,
otr.PROD_OIL_VOL prod_oil,
otr.ITEM_ID,
otr.ITEM_TYPE
FROM table_test otr
WHERE otr.ITEM_NAME IN ('SM')
AND otr.ITEM_TYPE='type_1' AND CAST(otr.START_DATETIME AS date) >= DATEADD(MONTH,DATEDIFF(MONTH,0,otr.START_DATETIME),0) AND CAST(otr.START_DATETIME AS date) <= '9/26/2017'
(SELECT MAX(PROD_OIL_VOL) FROM table_test
WHERE ITEM_NAME IN ('SM') AND ITEM_TYPE='type_1'
AND otr.ITEM_ID = ITEM_ID
AND CAST(START_DATETIME AS date) >= DATEADD(MONTH,DATEDIFF(mm,0,START_DATETIME),0)AND CAST(START_DATETIME AS date) <= '9/26/2017' )
AND ITEM_TYPE='type_1'
When you use this DATEADD(MONTH,DATEDIFF(MONTH,0,otr.START_DATETIME),0) in your WHERE clause you are telling SQL to include that row if the otr.START_DATETIME is greater than the 1st of the month for the otr.START_DATETIME column.
In other words - ALL ROWS will qualify on that statement...if otr.START_DATETIME is 2014-06-22 then the first of that is 2014-06-01.
To insure that you are only looking at the current month:
WHERE otr.START_DATETIME >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) -- First of THIS month
AND otr.START_DATETIME < CAST(GETDATE() AS DATE) -- less than midnight TODAY
Did the same as mentioned still having the same results showing date 8/1/2017 instead of 9/1/2017.( the last row is placed on 9/26/2017 and current date is 9/26/2017 right now)
Where clause
AND otr.START_DATETIME >= DATEADD(MONTH, DATEDIFF(MONTH, 0, otr.START_DATETIME ), 0)
AND otr.START_DATETIME < CAST(GETDATE() AS DATE)
Sub Query
AND START_DATETIME >= DATEADD(MONTH, DATEDIFF(MONTH, 0, START_DATETIME ), 0) AND START_DATETIME < CAST(GETDATE() AS DATE)
Re-read what I posted and compare to what you are using...
You still have the column in the calculation for the first of the month. That calculation will give you the first of the month of the start date, not the first of this month.
When you include a row with a start date in August, the results will return the first of August.