Solved

query question about dates

Posted on 2016-07-21
5
29 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 32

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

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 …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now