Go Back   CodingForums.com > :: Server side development > MySQL > Other Databases

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2006, 09:54 AM   PM User | #1
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,432
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
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.
esthera is offline   Reply With Quote
Old 12-14-2006, 02:43 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
if you are using TOP then you aren't using MySQL. I'll move the thread to the general database area.
guelphdad is offline   Reply With Quote
Old 12-14-2006, 08:18 PM   PM User | #3
nikkiH
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
nikkiH is on a distinguished road
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/
nikkiH is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:03 PM.


Advertisement
Log in to turn off these ads.