Solved

query question about dates

Posted on 2016-07-21
5
41 Views
Last Modified: 2016-08-08
I have a bunch or transactions that are stamped with a date time.
i want to select all the transactions that happened on a certain date.  it seems like when i do this though, that the minutes and seconds
of the date stamp are throwing off my select variable and returning no values.

for example

column addeddate has value of '2016-07-21 08:55:55:888'
and i query to return all rows that have an addeddate of '2016-07-21'

i get no hits on my results

whats an efficient way to get through this besides using greater than and less than or between.
0
Comment
Question by:jamesmetcalf74
[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
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41723810
Not quite sure what the question is, but add this to the WHERE clause of your statement

WHERE CAST(addeddate as date) = '2016-07-21'  -- can also use '20160721'
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 41723813
>besides using greater than and less than or between.
While we're on the subject, if you did want to use less than this would be the most appropriate way to do that...

WHERE addeddate >= '20160721' AND addeddate < '20160722'

SQL Server expert PortletPaul wrote a big honkin' aritcle on this called Beware of Between.
1
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 125 total points
ID: 41723815
E.g.

SELECT *
FROM yourTable
WHERE addeddate >= '20160721' AND addeddate < '20160722';

Open in new window


This works, cause the time portion of the boundary values is 00:00:00. See also Tibor's ultimate date and time guide.
1
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 41723929
please do not "alter data" to suit the selection
          instead
"alter the selection" to suit the data


e.g.        WHERE addeddate >= '20160721' AND addeddate < '20160722'

here "the selection" is altered and "the data" is unchanged


This approach to selection allows the query optimizer to make best use of indexes for performance. If you alter data, particularly by using functions, you can remove the ability to access indexes.
0
 

Author Closing Comment

by:jamesmetcalf74
ID: 41747346
Thanks Gentlemen
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

623 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