• minimay (5/6/2015)


    Im not trying to solve anything Im trying to figure out how to do this for future reference

    Dealing with dates in sql is a topic that many people don't understand and as a result they create very poor performing queries, or queries that simply don't work. I would recommend changing your query to something more like this. Aside from the difference in evaluating the date also notice that I specified the size of your varchar. If you don't do this it will be the default length. This default changes based on how it is used. In your case it would be 30 but in other cases it will be 1.

    Declare @Julycount int

    Select @Julycount = Count(*)

    From orders

    Where OrderDate >= '2015-07-01'

    and OrderDate < '2015-0801'

    print 'The total orders for july is ' + Cast(@JulyCount as varchar(4))

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/