Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

query question about dates

Posted on 2016-07-21
5
Medium Priority
?
43 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 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 1000 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 36

Assisted Solution

by:ste5an
ste5an earned 500 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 500 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

877 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