SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DatePart


DatePart

Author
Message
Manchu Mike
Manchu Mike
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 85
I'm trying to create a view of item quantities with due dates. I want to total each item by month. I'm using date part as below. When I get out to +9, the next year (2010) it will return 0. How do I account for the next year? Thanks

SUM(CASE WHEN DATEPART(MONTH, scheddate) = DATEPART(MONTH, GetDate()) THEN qty ELSE 0 END) AS month_current, SUM(CASE WHEN DATEPART(MONTH, scheddate) = DATEPART(MONTH, GetDate()) + 1 THEN qty ELSE 0 END) AS month_2
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38917 Visits: 38508
Really need more information to help you. It would help if you would take the time read and follow the guidelines in the first article I reference below in my signature block. If you would like to know more as to why, go to my blog (SQL Musing from the Desert) and read the entry on "The Flip Side" (that is just part of the title).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)

Group: General Forum Members
Points: 801 Visits: 2144
you need to use the dateadd with convert functions

select
SUM(CASE WHEN DATEPART(MONTH, scheddate) = DATEPART(MONTH, GetDate()) THEN qty ELSE 0 END) AS month_current,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 1, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_2,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 2, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_3,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 3, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_4,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 4, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_5,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 5, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_6,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 6, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_7,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 7, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_8,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 8, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_9,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 9, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_10,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 10, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_11,
SUM(CASE WHEN left(convert(varchar, scheddate, 112), 6) = left(convert(varchar, dateadd(month, 11, GetDate()), 112), 6) THEN qty ELSE 0 END) AS month_12
from (
select getdate() as scheddate, 1 as qty
union all
select '20090605' as scheddate, 1 as qty
union all
select '20090605' as scheddate, 1 as qty
union all
select '20091105' as scheddate, 1 as qty
union all
select '20100205' as scheddate, 1 as qty
) T1


Manchu Mike
Manchu Mike
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 85
Samuel,

This is exactly what I needed, works great! thank you very much.
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)SSC Eights! (801 reputation)

Group: General Forum Members
Points: 801 Visits: 2144
mike (5/14/2009)
Samuel,

This is exactly what I needed, works great! thank you very much.


No probs :-D

Date comparisons can throw up a few of these gotchas for Newbies!
Have a careful look at the date functions and the convert function in books online. When working with any business data, being able to acurately deal with dates is a must.
mm-1009269
mm-1009269
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 182
Thanks for this post Samuel! This is something I can use too...

Michelle :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search