Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: todays date

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts

    todays date

    can you help me?
    I want to query as follows:

    select top 1 * from tableb where date=now()

    I think my sytax is wrong but date is a datetime field and I want to select records that match todays date -- how do I do this?

    also what syntax should i use when i insert into tableb(date)values (datefield)

    please advise -- i only want to look at date and now time.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    if you are using TOP then you aren't using MySQL. I'll move the thread to the general database area.

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by esthera View Post
    can you help me?
    I want to query as follows:

    select top 1 * from tableb where date=now()

    I think my sytax is wrong but date is a datetime field and I want to select records that match todays date -- how do I do this?
    Using SQL Server?
    You don't want an = because of the milliseconds. And I think "date" is a reserved word, so if that's your field name, use brackets. You want something like this.

    This covers the last 24 hours.
    Code:
    select top 1 * from tableb where [date] > getdate()-1 and [date] <= getdate()
    This covers "today" but note that converting slows down queries pretty bad, depending on indexes (I work with huge tables; this may not be an issue for you). The bigger the table, the worse this is.
    Code:
    where [date] >= convert(varchar,getdate(),101)
    Quote Originally Posted by esthera View Post
    also what syntax should i use when i insert into tableb(date)values (datefield)
    did you have a problem with that?
    Use brackets and try again.
    insert into tableb([date]) values (getdate())

    You may have a problem using getdate() in a user defined function or stored procedure.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •