?
Solved

SELECT Statement Doesn't Get Records With Dates

Posted on 2012-04-11
4
Medium Priority
?
309 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
[X]
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
4 Comments
 
LVL 2

Accepted Solution

by:
Kelden earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

801 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