Solved

SELECT Statement Doesn't Get Records With Dates

Posted on 2012-04-11
4
300 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
ID: 37831885
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
ID: 37831951
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
ID: 37832050
Fantastic that works like a charm.  Can't believe I didn't think of that myself!
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 37832059
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

773 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