Parameter sniffing issue with different minutes as opposed to using midnight

  • Hej,

    I'm not facing this issue but as I was working another parameter sniffing issue I came to think about this and if it might cause an issue?

    So I'm more looking for your experience in the past and more an answer of kind "Nah, you'r good" or "You should definitely go with rounding to whole hours or whole dates".

    Example (Parameters = @FromDate and @ToDate):

    EXEC Myproc '2024-10-14 08.45.00', '2025-10-14 08.45.00'

    EXEC Myproc '2024-10-14 08.47.00', '2025-10-14 08.47.00'

    EXEC Myproc '2024-10-14 08.53.00', '2025-10-14 08.53.00'

    EXEC Myproc '2024-10-14 08.59.00', '2025-10-14 08.59.00'

    As the above is a period of a year it hasn't to be exact when it comes down to how many rows are returned.

    OR

    EXEC Myproc '2024-10-14 00.00.00', '2025-10-15 00.00.00'

  • Firstly, it is safer to use named parameters when calling a proc.  If somebody adds a new parameter between the existing ones, you have to go and find and edit every call to the proc.

    EXEC Myproc @FromDate='2024-10-14 00.00.00', @ToDate='2025-10-15 00.00.00'

    Then, inside the proc you want to use

    WHERE YourDateTime >= @FromDate AND YourDateTime < @ToDate
  • Thank you for the feedback! Yes, I do always use named parameters. This was just to try to make it clearer here but ...

    My question really was if someone has seen an issue with entering parameters that has minutes and second scattered all over the day (when timespan between @FromDate and @ToDate is months or years) instead of just having it down to midnight like ..

    @FromDate = '2024-10-14 00:00:00', @ToDate = '2025-10-15 00:00:00'

    ... as opposed to ...

    @FromDate = '2024-10-14 10:45:32', @ToDate = '2025-10-14 02:34:21'
    @FromDate = '2024-10-14 16:18:19', @ToDate = '2025-10-14 14:13:56'
    @FromDate = '2024-10-14 03:36:00', @ToDate = '2025-10-14 09:26:53'

    And this is if we're talking time span of months or years. Not perhaps within one single day.

    Cheers

  • If you are looking for data for a full month or a full year, then use the start of that year(or month) to the start of the next year(or month).  That way you always get all of the data in the window.

  • As DesNorton notes, if you want a full period then normalize to midnight, as opposed to someone using sysdatetime or DateTime.Now() as a parameter. I would document and decide if you're doing full days or not. Someone entering this:

    @FromDate = '2024-10-14 00:00:00', @ToDate = '2025-10-15 00:00:00'

    Might want

    @FromDate = '2024-10-14 00:00:00', @ToDate = '2025-10-15 11:59:59'

    Or really, < 2025-10-16 00:00:00

     

  • Thanks guys for you advices 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply