CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    116

    Using Month and year only to get all data

    I would like to find out if extraction of data from database is possible by using the mm/yyyy only.

    My code is as below but it is not a workable code:

    Code:
    SELECT vessel, voyage
    FROM dneirout 
    WHERE CONVERT(DATETIME, dneirout.CONTAINER_OUT_DATE, 103) >=  CONVERT(DATETIME, '09/2008', 103)

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Using Month and year only to get all data

    The 3rd paramter to CONVERT is used only when you convert to a character type, so it won't work in your case.
    Instead, you should use DATEDIFF function like this:
    Code:
    SELECT vessel, voyage
    FROM dneirout 
    WHERE DATEDIFF(month,'20080901',dneirout.CONTAINER_OUT_DATE)>=0
    Check the DATEDIFF function specs in BOL for more details.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Using Month and year only to get all data

    What database are you using?

    If it is SQL Server then look at DatePart function. This function will let you divide the date into Month, Day and Year.

  4. #4
    Join Date
    Jul 2007
    Posts
    116

    Re: Using Month and year only to get all data

    Hi, i am using Microsoft SQL Server Management Studio Express,

    Maybe I try defining my problem here, I would like to do a monthly consolidation of records if the user intend to look at .for example September 2007, so i will extract all the data from 1/09/2007 to 31/09/2007.

    HTH
    Last edited by hsteo; May 9th, 2008 at 04:36 AM.

  5. #5
    Join Date
    Jun 2006
    Posts
    437

    Re: Using Month and year only to get all data

    Hi all

    A part from September constains 30 days, no 31, you should use DatePart function, as Shuja Ali said.
    So a query that solves your problem may be
    Code:
    SELECT vessel, voyage
      FROM dneirout
     WHERE DatePart('m', CONTAINER_OUT_DATE) = 9
       AND DatePart('yyyy', CONTAINER_OUT_DATE) = 2007

  6. #6
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Using Month and year only to get all data

    Quote Originally Posted by hsteo
    Hi, i am using Microsoft SQL Server Management Studio Express,

    Maybe I try defining my problem here, I would like to do a monthly consolidation of records if the user intend to look at .for example September 2007, so i will extract all the data from 1/09/2007 to 31/09/2007.
    HTH
    This will extract records in sept.:
    Code:
    SELECT vessel, voyage
    FROM dneirout
    WHERE
    DATEDIFF(month,CONTAINER_OUT_DATE,'20070901')=0
    Also the solution that davide++ mentioned will work.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

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