Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to return data in monthwise in a year from current date.
i.e if current date is 2012-08-27 the data should be return from 2012-01-01 to 2012-08-27.
so here i am not able to get the first month of the current year i.e "jan 2012"
please give required query. The following query is to get monthwise data plese find out where i have done wrong.

SQL
SELECT
           DATEPART(MONTH, convert(DATETIME,CreatedDateTime,13)) AS [Month of Year]
         , Id AS [TotalShipOrders]
      FROM ShipmentDetail
      WHERE CONVERT(VARCHAR(7),CreatedDateTime,111)= convert(varchar(7),@CurrentDate,111)
      UNION ALL
      SELECT Month, NULL
      FROM @Months



Regards,
Raghu.
Posted
Updated 26-Aug-12 19:41pm
v2

hi,

first month of the any year will always same.
so you had to just find current year and concate string. then convert that string to datetime. so will find desired date.

Thanks,
Viprat
 
Share this answer
 
Comments
Madhugundi 27-Aug-12 1:38am    
i am not able to get u please view my query this should return monthwise data where i have done mistake please find out.

SELECT
DATEPART(MONTH, convert(DATETIME,CreatedDateTime,13)) AS [Month of Year]
, Id AS [TotalShipOrders]
FROM ShipmentDetail
WHERE CONVERT(VARCHAR(7),CreatedDateTime,111)= convert(varchar(7),@CurrentDate,111)
UNION ALL
SELECT Month, NULL
FROM @Months
select DATEADD(Year,-1*DateDiff(Year,getdate(),0),0)
 
Share this answer
 
to get first date of current year,
see this example
SQL
select cast('01 jan' + CAST((DATEPART(year, getdate())) as varchar) AS DATETIME);

instead of getdate() you can pass any date here.
Happy Coding!
:)
 
Share this answer
 
try below query:-

if @currentdate is 2012-08-27
below query returns all the data for year 2012

SQL
SELECT DATEPART(MONTH, convert(DATETIME,CreatedDateTime,13)) AS [Month of Year],
      Id AS [TotalShipOrders]
      FROM ShipmentDetail
      WHERE datepart(year,CreatedDateTime)=datepart(year,@CurrentDate)    UNION ALL
      SELECT Month, NULL
      FROM @Months
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900