• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Select with DateTime where condition

Hi,

I need to select any record after a given date ...
The where filed (TimeStamp)is typeof datetime - how can I do my query?
SELECT [ID]
      ,[Timestamp]
  FROM [dbo].[AllRecords] WHERE [Timestamp]>2011-06-30 23:59:59.000

does not work ...

Thanks

Andre
0
andre72
Asked:
andre72
  • 4
  • 2
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
DECLARE @Date DATETIME
SET @Date = '2011-06-30 23:59:59.000'
SELECT [ID]
      ,[Timestamp] FROM [dbo].[AllRecords] WHERE [Timestamp > @Date

Open in new window


Raj
0
 
Rajkumar GsSoftware EngineerCommented:
If you mean to want to fetch those records with date which is after the date you mention, you can try this query

DECLARE @Date DATETIME
SET @Date = '2011-06-30 23:59:59.000'
SELECT [ID]
      ,[Timestamp]
  FROM [dbo].[AllRecords] WHERE DATEDIFF(d,@Date , [Timestamp])> 0

Open in new window


Raj
0
 
andre72Author Commented:
I get an error like before:
While converting from varchar datatyp to datetime datatype a value is outside
The conversion of a varchar data type to a datetime data type of the value is out of range. (translated from German)

But Timestamp is for sure datetime ...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Rajkumar GsSoftware EngineerCommented:
I think the date format is different.

Try this line as the first line
 SET DATEFORMAT MDY

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you should read this article:
http://www.experts-exchange.com/A_1499.html

so, above suggestion is almost fine, but still implicit data type conversion:
SET @Date = '2011-06-30 23:59:59.000'
should be:
SET @Date = convert(datetime, '2011-06-30 23:59:59.000', 120)

hope this helps
0
 
andre72Author Commented:
Strange you got it ...
0
 
Rajkumar GsSoftware EngineerCommented:
Glad I could help you
Raj
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now