Solved

query question about dates

Posted on 2016-07-21
5
35 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
5 Comments
 
LVL 65

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 65

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 33

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 48

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 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

11 Experts available now in Live!

Get 1:1 Help Now