Home Forums SQL Server 2008 SQL Server Newbies convert integer datatype as date format and group them by month RE: convert integer datatype as date format and group them by month

  • I would do something like this:

    with basedata as (

    select

    Customer,

    dateadd(day,[Date],'20040630') SalesDate,

    Sales

    from

    dbo.YourTable

    )

    select

    Customer,

    dateadd(month,datediff(month,0,SalesDate),0) CalendarMonth,

    sum(Sales) MonthlySales

    from

    basedata

    group by

    Customer,

    dateadd(month,datediff(month,0,SalesDate),0)

    order by

    Customer,

    dateadd(month,datediff(month,0,SalesDate),0);