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

x
?
Solved

get date in sql

Posted on 2014-04-01
5
Medium Priority
?
422 Views
Last Modified: 2014-04-01
Hi all,
I am looking to get the date part only of a field in sql without the time, for example I have a field named DateSent and it actually stores date and time, now I want to see all records who DateSent was today or in the last two days, how do I accomplish that?
0
Comment
Question by:bfuchs
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39971142
select *
from MyTable T
where T.DateSent >= convert(datetime, convert(varchar(11), dateadd(day, -2, getdate()), 113), 113)
0
 
LVL 4

Author Comment

by:bfuchs
ID: 39971157
Hi,
this is for the last two days, how do I get for today only?
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 1400 total points
ID: 39971163
Just change the number of days you subtract...

declare @NumDays int

set @NumDays = 2

-- Shows 2 full days + today
select *
from MyTable T
where T.DateSent >= convert(datetime, convert(varchar(11), dateadd(day, -1*@NumDays, getdate()), 113), 113)

set @NumDays = 0

-- Shows today
select *
from MyTable T
where T.DateSent >= convert(datetime, convert(varchar(11), dateadd(day, -1*@NumDays, getdate()), 113), 113)

Open in new window

0
 
LVL 4

Author Comment

by:bfuchs
ID: 39971183
well, I am posting here the sql I have in access and the equivalent sql you're suggesting in sql, and I get completely different results.

Access
SELECT EmployeesEmailMessages.EmployeeID, Count(EmployeesEmailMessages.ID) AS CountOfID
FROM EmployeesEmailMessages INNER JOIN EmailMessagesTbl ON EmployeesEmailMessages.EmailMessagesID = EmailMessagesTbl.ID
WHERE (((EmailMessagesTbl.DateCreated)>=Date()))
GROUP BY EmployeesEmailMessages.EmployeeID
HAVING (((Count(EmployeesEmailMessages.ID))>1));

Open in new window


sql
SELECT     dbo.EmployeesEmailMessages.EmployeeID
                            FROM          dbo.EmployeesEmailMessages INNER JOIN
                                                   dbo.EmailMessagesTbl ON dbo.EmployeesEmailMessages.EmailMessagesID = dbo.EmailMessagesTbl.ID
                            WHERE      (dbo.EmailMessagesTbl.DateCreated >= convert(datetime, convert(varchar(11), dateadd(day, -1, getdate()), 113), 113) 
)
                            GROUP BY dbo.EmployeesEmailMessages.EmployeeID
                            HAVING      (COUNT(dbo.EmployeesEmailMessages.ID) > 1)

Open in new window

0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 39971191
actually, I was comparing to the wrong option, indeed it works with 0, quick job, thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

926 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