Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SELECT Statement Doesn't Get Records With Dates

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

839 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