0

Hoping someone can help. I've looked on the internet for a solution but none seem to resolve it.

I want to query a large table and only get the results back where a column equals today's date.

Here's the query:

select [Table1].[Field1]
from [Table1]
where [Table1].[Field1] = GetDate()

The date format is as follows:

 20020630

I'm a beginner in SQL so any help would be really appreciated because I am growing fond of it.

Thank you!!! :)

2
  • What datatype is field1? Commented Aug 26, 2011 at 7:55
  • Im not sure becasue the table is on a linked server and i dont have full access to it, sorry. Commented Aug 26, 2011 at 7:56

1 Answer 1

1

To find the broken value:

select [Table1].[Field1]
from [Table1]
where ISDATE([Table1].[Field1]) = 0

GETDATE includes a time, so you need to remove this. This assumes SQL Server 2008+

select [Table1].[Field1]
from [Table1]
where [Table1].[Field1] = CAST(GetDate() AS date)
Sign up to request clarification or add additional context in comments.

7 Comments

Its SQL Server 2008 R2. Ive just tried the GetDate solution and that says the following:
Msg 243, Level 16, State 1, Line 1 Type date is not a defined system type.
And the first solution (ISDATE) doesnt return any values.
@PDB: date is a valid type in SQL Server 2008+. Your linked server must be SQL Server 2005 or earlier and this code is executing there. You need to know datatype for Field1.
Is there any way to find tht out by using a query?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.