Solved

get date in sql

Posted on 2014-04-01
5
418 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Results to Excel File 18 90
Format Date fields 11 65
SQL Server how to use a VARIABLE to link tables in a SQL Script? 3 41
SQL multiple joins 2 21
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.

710 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