Solved

query question about dates

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

831 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