Computing.Net > Forums > Programming > SQL Query

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SQL Query

Reply to Message Icon

Name: secrets
Date: November 2, 2004 at 21:44:17 Pacific
OS: WINDOWS
CPU/Ram: 1.7
Comment:

i have a datatime field which has both date and time value now i want to get the records on the basis of date only...
i have tried query "select dd,aa from table where datetime=#11/03/2004#" but it is not working..



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: November 3, 2004 at 05:58:34 Pacific
Reply:

It depends on what kind of database you're using...It looks like either access or sql server.

You probably need the DatePart function.

Good luck,
-SN


0

Response Number 2
Name: Chi Happens
Date: November 3, 2004 at 06:27:02 Pacific
Reply:

You can still just use the date (if it is actually a datetime type)

So...
SELECT dd,aa FROM TABLE WHERE DateTime='11/03/2004'

is the same as

SELECT dd,aa FROM TABLE WHERE DATEPART(day,DateTime)=3 AND DATEPART(month,DateTime)=11 AND DATEPART(year,DateTime)=2004

If, however, the field is not of a datetime type, but a char or varchar, then you will need to either cast it as a datetime or use a wildcard:

CAST EXAMPLE:
SELECT dd,aa FROM TABLE WHERE CAST(DateTime AS DATETIME)='11/03/2004'

WILDCARD EXAMPLE:
SELECT dd,aa FROM TALBE WHERE DateTime LIKE '11/03/2004%'

Hope this helps
Chi

"They mostly come at night...mostly"


0

Response Number 3
Name: secrets
Date: November 3, 2004 at 12:40:46 Pacific
Reply:

right! i am using access and the database field has the date and time both values in it now suppose i want to view the records of 11/2/2004 there are records in database but by calling the query
SELECT Logs.dnumber,Logs.duration, Logs.consumed_units, Logs.cardserial, Logs.callcharges, Logs.datetime FROM Logs WHERE ((Logs.datetime)='11/2/2004')
it gave me type mismatch error.....
and if i remove the '' from the date it gave no error but gives that no record found.


0

Response Number 4
Name: SN
Date: November 3, 2004 at 15:42:33 Pacific
Reply:

The type mismatch error your getting is because you're trying to convert a string to a date. Use # instead of ', like you did in your first post.

I just tried it in access, Chi's first method only returned the rows that have the time portion set to 12:00 AM. It may work in SQL server...I'll have to try it.

To get the result I wanted, I had use Chi's second query (using the equivalent day, year, month functions):

SELECT * from payments where Year(payment_date)=Year(#1/6/04#) and Month(payment_date)=Month(#1/6/04#) and Day(payment_date)=Day(#1/6/04#)

Shame on MS for not having a function to extract the entire date...I thought datepart would be able to do it in one call.

-SN


0

Response Number 5
Name: Chi Happens
Date: November 4, 2004 at 12:38:58 Pacific
Reply:

Yeah well access is a lot more weird than SQL server.

SELECT Table1.ID, Table1.mydate
FROM Table1
WHERE (((Table1.mydate)=#1/6/2004#));

works as long as the data is 1/6/2004

it fails on a data that has time in it as well. ACCESS SUCKS

Here is my sample info:

Table Name is Table1
Field1 is ID (autoid primary key)
Field2 is MyDate (datetime)

DATA:
FIELD1 FIELD2
1 11/10/2004 10:00:00 AM
2 1/6/2004
3 1/6/2004 10:00:00 PM

the query above returns this:

FIELD1 FIELD2
2 1/6/2004

but it should return this:

FIELD1 FIELD2
2 1/6/2004
3 1/6/2004 10:00:00 PM

this access query return the correct expected data:

SELECT Table1.ID, Table1.mydate
FROM Table1 WHERE DATEPART('m',mydate)=1 and datepart('d',mydate)=6 and datepart('yyyy',mydate)=2004;


I restate my first observation:
ACCESS SUCKS.

Chi


"They mostly come at night...mostly"


0

Related Posts

See More



Response Number 6
Name: secrets
Date: November 5, 2004 at 01:16:57 Pacific
Reply:

thanks guys this helps alot.


0

Response Number 7
Name: secrets
Date: November 8, 2004 at 11:55:17 Pacific
Reply:

what if i want to get the time only fromt the datatime field? only time..


0

Response Number 8
Name: Chi Happens
Date: November 9, 2004 at 04:09:40 Pacific
Reply:

uses datepart as well but instead of month day and year, use hour, minute, seconds.

Chi



0

Response Number 9
Name: secrets
Date: November 9, 2004 at 10:32:09 Pacific
Reply:

thanks Chi.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: SQL Query

Access sql query mystery!! www.computing.net/answers/programming/access-sql-query-mystery/12793.html

SQL Query www.computing.net/answers/programming/sql-query/16559.html

Need SQL query help. www.computing.net/answers/programming/need-sql-query-help/13097.html