Solved

SELECT Statement Doesn't Get Records With Dates

Posted on 2012-04-11
4
295 Views
Last Modified: 2012-04-11
I have a table which is populated with production data from our shop floor.  Part of what I'm trying to do is get all records from yesterday so we can output what has gone on the day before.


I have the following procedure:
CREATE PROCEDURE [dbo].[sGetRecords]
(
	@DateFrom CHAR(24)
)

AS
SELECT *
  FROM dbo.MyTable
WHERE
	MyTable.ProductionDate >= @DateFrom

Open in new window


However when I call this no records are returned for the previous day (format: 2012-04-10 12:24:13.000), the only way I can get records is if I change it to <= @DateFrom but this returns everything.

I am passing in the date paramter as: 2012-04-10 00:00:00

I have even tried doing a BETWEEN the date parameter and the result of GetDate() however this also brings back nothing.

I'm probably missing something completely obvious, but I've been looking at this for a while now.
0
Comment
Question by:angus_young_acdc
4 Comments
 
LVL 2

Accepted Solution

by:
Kelden earned 500 total points
Comment Utility
First you should try to declare your variable as datetime

CREATE PROCEDURE [dbo].[sGetRecords]
(
	@DateFrom DATETIME
)

AS
SELECT *
  FROM dbo.MyTable
WHERE
	MyTable.ProductionDate >= @DateFrom

Open in new window


Will this give you a correct result?
SELECT *
  FROM dbo.MyTable
WHERE
	MyTable.ProductionDate >= GetDate() - 1

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
how is

MyTable.ProductionDate

defined?

What sort of data is in the field, is it a date field?
0
 
LVL 15

Author Closing Comment

by:angus_young_acdc
Comment Utility
Fantastic that works like a charm.  Can't believe I didn't think of that myself!
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

firstly, you are passing

@DateFrom char(24)

char datatype always takes extra blank.
e.g. if @DateFrom = '2012-04-01'

then with char datatype it will be '2012-04-01              '  extra spaces....

so either change datatype to datetime

or change ur query.

ProductionDate >= convert(datetime, RTRIM(CAST @DateFrom AS VARCHAR(30))), 103)

This will give result.

- Bhavesh
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now