Results 1 to 12 of 12

Thread: Sql Statement Help

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Resolved Sql Statement Help

    I need to select back a months data from an access database where the date is a certain month and a certain year. The date is date/time datatype and is in US format in the database.I was thinking I'd be able to use some wildcards but not sure how to manipulate these in my where clause!

    Any ideas?
    Last edited by helpmeplease; Feb 28th, 2005 at 04:38 PM.

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

    Re: Sql Statement Help

    Quote Originally Posted by helpmeplease
    I need to select back a months data from an access database where the date is a certain month and a certain year. The date is date/time datatype and is in US format in the database.I was thinking I'd be able to use some wildcards but not sure how to manipulate these in my where clause!

    Any ideas?
    We do MS SQL here - not ACCESS...

    But have you looked into "WHERE DATEPART(yyyy,DATEFIELD)=2005 and DATEPART(mm,DATEFIELD)=2" for selectiong February 2005 entries?

    You can also do WHERE DATEFIELD>='2005-02-01' and DATEFIELD<'2005-03-01' - although I believe in ACCESS the delimiter for a date is #-sign - right?

    *** 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

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Re: Sql Statement Help

    Well it's my query in vb i'm trying to write

    I was thinking of something like :

    Where date like '2%' and like '%2005' but not sure how to write it.

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

    Re: Sql Statement Help

    Quote Originally Posted by helpmeplease
    Well it's my query in vb i'm trying to write

    I was thinking of something like :

    Where date like '2%' and like '%2005' but not sure how to write it.
    What do you mean - my query in VB?

    Post some code - what are you using?

    *** 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

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Re: Sql Statement Help

    I'm not sure what you meant by ms sql.


    gstrsql = "Select * FROM Payment Where Date Like '2%' and Like '%2005' "

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

    Re: Sql Statement Help

    Quote Originally Posted by helpmeplease
    I'm not sure what you meant by ms sql.


    gstrsql = "Select * FROM Payment Where Date Like '2%' and Like '%2005' "
    Maybe something like:

    Code:
    gstrsql = "Select * From Payment Where [Date] >= #2004-01-01# and [Date] <= #2004-12-31#"
    I believe that in ACCESS you have to delimit your dates with #-signs.

    We use MS SQL SERVER 2000 - another database product from MS - it's syntax a bit different.

    With MS SQL SERVER we would have used:

    Code:
    gstrsql = "Select * From Payment Where DatePart(yyyy,[Date])=2004"
    I'm putting [DATE] in []-brackets because I think that DATE is a reserved keyword - is that really you FIELD NAME for the 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

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Re: Sql Statement Help

    The month could be any month so the number of days will be different depending on the month selected so that's why i was thinking of where the date like 2(ie. february) with more data after it and ends with 2005 with data before the 2005.In other words 02... /??/... 2005

    Dunno if I'm explaining it very well.

    I don't want to hard code the month and year.they are selected through comboboxes and assigned to variables.

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

    Re: Sql Statement Help

    Quote Originally Posted by helpmeplease
    The month could be any month so the number of days will be different depending on the month selected so that's why i was thinking of where the date like 2(ie. february) with more data after it and ends with 2005 with data before the 2005.In other words 02... /??/... 2005

    Dunno if I'm explaining it very well.

    I don't want to hard code the month and year.they are selected through comboboxes and assigned to variables.
    Does ACCESS have a DATEPART function??

    If so, then ask for the YEAR and then the MONTH and build the SQL string like I suggested.

    I never said to hardcode.

    Post some actual code from your app - so we can work on that.

    *** 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

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Re: Sql Statement Help

    Yeah Access has a DATEPART function so i'll give that a shot and see how that goes.

    all i'm pretty much doing is getting the month value 1-12 from combobox and year and assigning these to a variable and hoping to utilise them someway to get specific data from Payment table.

    There isn't much code to show.

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

    Re: Sql Statement Help

    Then it should be something like:

    Code:
    strSql = "Select * From Payment" _
        & " Where DatePart(yyyy,[Date])=" & cboYear.Text _
        & " and DatePart(mm,[Date])=" & cboMonth.Text
    Good luck.

    *** 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

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    49

    Re: Sql Statement Help

    gstrSql = "SELECT * From Payment " _
    & "WHERE DatePart('yyyy',[Date_Received]) = " & cboYear.List(cboYear.ListIndex) _
    & " AND DatePart('m',[Date_Received]) =" & (cboMonth.ListIndex + 1)

    This works

    Cheers for the help

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

    Re: Sql Statement Help

    No problem - glad it worked for you...

    *** 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

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