Results 1 to 19 of 19

Thread: [RESOLVED] getting from date of week to date of week

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Resolved [RESOLVED] getting from date of week to date of week

    Hello.
    I'm trying to get a date of week to date of week in SQL.

    ON vb it can be done like this:
    Code:
      Dim dt As DateTime = DateTime.Now.AddHours(-6).[Date]
            Dim delta As Integer = DayOfWeek.Thursday - dt.DayOfWeek
            If delta > 0 Then
                delta -= 6
            End If
            Dim dtFrom As DateTime = dt.AddDays(delta)
            Dim dtTo As DateTime = dtFrom.AddDays(6)
    So I'm trying to get a Thursday starting date and a + days plus on end date.
    So on end date I can just add +6 on the starting date. But how can I get the Thursday day of the current week?
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: getting from date of week to date of week

    Dirty but works
    VB Code:
    1. Dim dtDate As Date = Now
    2.         If dtDate.DayOfWeek = DayOfWeek.Thursday Then
    3.             MsgBox("ZGot")
    4.         Else
    5.             MsgBox("Not")
    6.         End If
    7.         Do Until dtDate.DayOfWeek = DayOfWeek.Thursday
    8.             dtDate = dtDate.AddDays(1)
    9.         Loop
    10.         MsgBox(dtDate.DayOfWeek)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: getting from date of week to date of week

    Except that's not in SQL....
    Quote Originally Posted by TheOP
    I'm trying to get a date of week to date of week in SQL.
    I'm not sure I'm 100% clear on what the question is...
    Odds are though, it's going to involve DatePart, DateAdd and a loop of some kind.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: getting from date of week to date of week

    In SQL like this

    SQL Code:
    1. DECLARE @dtDate AS date = GETDATE(),
    2.         @DName VARCHAR(20)
    3.    
    4. SET @DName = DATENAME(WEEKDAY,@dtDate)
    5.  
    6. while @DName != 'Thursday'
    7.     BEGIN
    8.         PRINT @dtDate
    9.         SET @dtDate = DATEADD (d,1,@dtDate)
    10.         SET @DName = DATENAME(WEEKDAY,@dtDate)
    11.         PRINT @dtDate
    12.     END
    13.  PRINT Datename(weekday,@dtDate)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    No loop needed - simple formula - put it in a scalar UDF (obviously put the DATEPART value into a vbl and use it in the case statement...)

    Code:
    Case When DatePart(dw, @TestDate) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @TestDate), @TestDate) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @TestDate)), @TestDate) End
    Here it is being tested.

    Code:
    Print DatePart(dw, '2016-03-24') -- Thursday (5)
    Print DatePart(dw, '2016-03-25') -- Friday (6)
    Print DatePart(dw, '2016-03-26') -- Saturday (7)
    Print DatePart(dw, '2016-03-27') -- Sunday (1)
    Print DatePart(dw, '2016-03-28') -- Monday (2)
    Print DatePart(dw, '2016-03-29') -- Tuesday (3)
    Print DatePart(dw, '2016-03-30') -- Wednesday (4)
    Print DatePart(dw, '2016-03-31') -- Thursday (5)
    
    Declare @TestDate datetime
    Set @TestDate='2016-03-24'
    
    While @TestDate<'2016-04-10'
    Begin
    	Print Convert(varchar(10),@TestDate,101)
    	+' '+Convert(varchar(10),Case When DatePart(dw, @TestDate) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @TestDate), @TestDate) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @TestDate)), @TestDate) End,101)
    	Set @TestDate=DateAdd(dd,1,@TestDate)
    End
    And this is what the output results look like - two columns of dates - the date of the week and the THURDAY associated with that date of the week
    Code:
    5
    6
    7
    1
    2
    3
    4
    5
    03/24/2016 03/24/2016
    03/25/2016 03/24/2016
    03/26/2016 03/24/2016
    03/27/2016 03/24/2016
    03/28/2016 03/24/2016
    03/29/2016 03/24/2016
    03/30/2016 03/24/2016
    03/31/2016 03/31/2016
    04/01/2016 03/31/2016
    04/02/2016 03/31/2016
    04/03/2016 03/31/2016
    04/04/2016 03/31/2016
    04/05/2016 03/31/2016
    04/06/2016 03/31/2016
    04/07/2016 04/07/2016
    04/08/2016 04/07/2016
    04/09/2016 04/07/2016

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    Hmmm.
    This seems a little more complicated.
    I don't know if the initial post was enlightening.
    I'm actually trying to get the Thursday of the current week and add 6 so i can get Thursday to Wednesday.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    Ok.Sorry. Ignore.
    (damn).
    What I'm trying is to get the Thursday BEFORE the current Thursday comes along.
    So for example today we have Wednesday. I don'[t want to get the Thursday of the this week, so, not tomorrow.
    What I would have to get today is 03/24 to 03/30 . Tomorrow the correct get would actually by (after 23:59 Wednesday), 03/31 to 04/06 .
    Thanks.
    I'm thinking of vb-ing that as I can't mix anything on SQL.Don't know maybe I am missing something...
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    I've simulated the vb function so I'm doing something like the below.
    Is this vial?
    Code:
    Declare @dt as datetime
    Declare @dtFrom as datetime
    Declare @dtTo as datetime
    --set @dt = CONVERT(DATETIME, getdate(), 102)
    set @dt = DateAdd(Day, Datediff(Day,0, GetDate()), 0)
    Declare @delta as int
    set @delta = 5 -  DatePart(dw, GETDATE()) 
    --select @delta
    IF @delta > 0 
    Begin
    set @delta = @delta - 7
    End
    --select @dt
    	set @dtFrom = DATEADD(day,@delta,@dt)
    	set @dtTo = DATEADD(day,6,@dtFrom)
    --	select @dtFrom
    Last edited by sapator; Mar 30th, 2016 at 04:47 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    Quote Originally Posted by sapator View Post
    What I would have to get today is 03/24 to 03/30 . Tomorrow the correct get would actually by (after 23:59 Wednesday), 03/31 to 04/06 .
    Did you not see my two columns of dates - where I showed 03/24 to 03/30 with 03/24 showing next to each of the dates in that week?

    It's exactly what you asked for - and done quite often in financial systems (I've got a client with an every-TUESDAY selectmen meeting where they sign off on this weeks invoice warrants.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    Hi.
    Yes , show it but i got confused on the between part so I just borrowed some stuff from your function but created one more readable to me.
    I'm testing it right now but I just want a feedback if someone sees and error - issue , I might be missing.
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    It's just that single formula in the CASE statement.

    The other code was a loop to test it in a query window - that might have looked like crap but not part of what you would use in production.

    Just the CASE statement - in a UDF - would make sense to me.

    You are doing heavy lifting adding days to a date (repeatedly) when the NUMBER of the date in the week was the only key you needed to adjust your date.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    Here it is:

    Code:
    Create Function GiveMeThursday_F(@SomeDate datetime)
    Returns datetime
    as
    Begin
    	Declare @dw int
    	Set @dw=DatePart(dw,@SomeDate)
    	Return Case When @dw between 5 and 7
    			Then DateAdd(dd, 5 - @dw, @SomeDate) 
    			Else DateAdd(dd, -(2 + @dw), @SomeDate) End
    End
    Select dbo.GiveMeThursday_F('03-30-2016')
    Select dbo.GiveMeThursday_F('03-31-2016')
    and that returns

    Code:
    -----------------------
    2016-03-24 00:00:00.000
    
    (1 row(s) affected)
    
    
    -----------------------
    2016-03-31 00:00:00.000
    
    (1 row(s) affected)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    OK.
    I misunderstood.
    So in my case this?
    Quoting:
    Code:
    Case When DatePart(dw, @TestDate) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @TestDate), @TestDate) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @TestDate)), @TestDate) End
    ------

    Code:
    declare @dtFrom datetime
    declare @dtTo datetime
    set  @dtFrom = DateAdd(Day, Datediff(Day,0, GetDate()), 0)
    
    select Case When DatePart(dw, @dtFrom) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @dtFrom), @dtFrom) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @dtFrom)), @dtFrom) 
    end
    
    set @dtTo = DATEADD(day,7,@dtFrom)
    select @dtTo
    But there is an issue here:
    I'm needing the zero time values....
    so
    Code:
    set  @dtFrom = DateAdd(Day, Datediff(Day,0, GetDate()), 0)
    The problem here is that if i run it with getdate(), it's ok.
    If however I add a day ( getdate()+1 ) or so, then from date is wrong.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    You always need to do a CONVERT(varchar(10),GetDate(),101) to get rid of time before doing any + or - logic.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    OK but that will still bring wrong results.
    Code:
    declare @dtFrom datetime
    declare @dtTo datetime
    set  @dtFrom = CONVERT(varchar(10),GetDate()+1,101) 
    
    select Case When DatePart(dw, @dtFrom) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @dtFrom), @dtFrom) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @dtFrom)), @dtFrom) 
    end
    
    set @dtTo = DATEADD(day,7,@dtFrom)
    select @dtTo
    I'm expecting 04-06 and I get 04-07
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    You are adding 1 in the CONVERT?

    Then you are adding 7?

    You are doing the math - make something 6.

    Did you not see my two line UDF?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    OK, so this?
    Code:
    declare @dtFrom datetime
    declare @dtTo datetime
    set  @dtFrom = CONVERT(varchar(10),GetDate()+10,101) 
    
    set  @dtFrom =  Case When DatePart(dw, @dtFrom) between 5 and 7
    			Then DateAdd(dd, 5 - DatePart(dw, @dtFrom), @dtFrom) 
    			Else DateAdd(dd, -(2 + DatePart(dw, @dtFrom)), @dtFrom) 
    end
    
    set @dtTo = DATEADD(day,6,@dtFrom)
    select @dtFrom
    select @dtTo
    getdate does not matter, I'm just simulating a date in the future
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: getting from date of week to date of week

    That is giving you a Thursday to Wednesday run of dates.

    Is that what you want??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: getting from date of week to date of week

    Yep.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width