CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Location
    Visual Basic.NET 2005
    Posts
    2

    Smile SQL Select and date problem

    hello,

    I have a table with these columns:

    IDsales, salesDate, salesmanID, itemDesc

    I need an SQL querry to select all the sales from a certain date, with the summed sales groupped by salesmanID... The problem with the querry is that the data in the salesDate field is in a long format (with dd/mm/yyyy hh:mm:sss) and I dont know how to set the querry to disregard the "time" info, and take in account the "date" part only.

    I am just a novice in SQL - all help is welcome )

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: SQL Select and date problem

    SQL takes care of that automatically. It's stored as a DATA field, which also has a time stamp.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: SQL Select and date problem

    Quote Originally Posted by nka View Post
    hello,

    I have a table with these columns:

    IDsales, salesDate, salesmanID, itemDesc

    I need an SQL querry to select all the sales from a certain date, with the summed sales groupped by salesmanID... The problem with the querry is that the data in the salesDate field is in a long format (with dd/mm/yyyy hh:mm:sss) and I dont know how to set the querry to disregard the "time" info, and take in account the "date" part only.

    I am just a novice in SQL - all help is welcome )
    You do not mention which database you use, but if you use SQL Server there's a number of ways to do that.

    If you only want those on a specific date, you can either make your query take larger then midnight and less then midnight.
    Such as
    myDateField > '2009-12-24' and myDateField < '2009-12-25' which should net you all days on the 24th of December.

    You can change the query to use the DATEPART function to clean your date time for the time portion.
    http://msdn.microsoft.com/en-us/libr...5(SQL.80).aspx

    You can use the mathematical representation of the datetime to do the same (a bit more confusing to read)
    CAST(FLOOR(CAST(myDateField AS FLOAT)) AS DATETIME)
    which also returns the datetime cleaned for the time portion.

    And so on ....

  4. #4
    Join Date
    Sep 2009
    Location
    Visual Basic.NET 2005
    Posts
    2

    Smile Re: SQL Select and date problem

    Thanks for your help!

    I am using an Miscrosoft Access database, and here is my SQL Select command:

    SELECT salesDate, salesmanID, SUM(pricePaid) AS Expr1, itemDesc, IDSales
    FROM Sales
    GROUP BY salesDate, salesmanID, IDSales
    HAVING (salesmanID = ?)
    ORDER BY salesDate DESC

    but it returns no results? what am I missing?

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: SQL Select and date problem

    I'd guess it's this line:
    Code:
    HAVING (salesmanID = ?)
    please use code tags in the future
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: SQL Select and date problem

    I agree with dglienna - my guess would be the having clause.
    What should be the purpose of the having clause in your query?

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

Featured