Solved

get date in sql

Posted on 2014-04-01
5
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 350 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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