Solved

SELECT Statement Doesn't Get Records With Dates

Posted on 2012-04-11
4
297 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

21 Experts available now in Live!

Get 1:1 Help Now