Solved

Date and Time in MS SQL

Posted on 2006-06-28
5
283 Views
Last Modified: 2008-02-26
Hello,

I'm running this:

select ..... where Create > CONVERT(DATETIME, CONVERT(INT, GETDATE() -1)))

Create = Field Name

I'm getting June 28 and June 27... but I should get only June 27

I even tried this: CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE()-1 ))
and
CONVERT(DATETIME, CONVERT(INT, GETDATE() -1))

but I'm not getting only for June 27....    -1 should take me a day back..no? but it doesn't... can anyone help me with this please

Thanks


0
Comment
Question by:ezzadin
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:skacore
ID: 17004129
Hello,
  You may want to try

SELECT .... WHERE create > DATEADD(d, -1, GETDATE())

If you just want stuff from a day ago, it's a little cleaner.
0
 

Author Comment

by:ezzadin
ID: 17004164
still the same.... I'm getting some of June 27 and some of June 28

the first record start    2006-06-27 17:50:08.000

I have some records that are 2006-06-27 00:00:00.000 ... i should get those as well and not get June 28

Please help

Thanks

0
 
LVL 1

Accepted Solution

by:
skacore earned 250 total points
ID: 17004265
Ah, now I see, try this:

SELECT ....  create >= DATEADD(dd, -1, DATEDIFF(dd, 0, GETDATE())) and create < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
0
 
LVL 2

Assisted Solution

by:MyersA
MyersA earned 250 total points
ID: 17004309
Hi ezzadin,

When I compare dates, I usually get rid of the time and I only use the date part. You can try this:
SELECT .... WHERE
create >= convert(DateTime,convert(varchar(11),getdate()-1)) and       
create < convert(DateTime,convert(varchar(11),getdate()))


0
 

Author Comment

by:ezzadin
ID: 17004555
Thanks guys
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now