Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL datetime set to midnight

Posted on 2009-07-01
5
Medium Priority
?
679 Views
Last Modified: 2012-05-07
Hi all

There are two elements to this question, but both simple answers.

1)  I have a table of datetimes for which the time component is whenever the record was created.  I need a SQL update query to set them all to midnight.

ie to convert 01/05/2009 21:23:22 to 01/05/2009 00:00:00

2) I am running a query which gets all matching records between @fromdate and @todate.  Where these are string parameters from user input.

How can I add a time to the parameter like this:

@fromdate must be @fromdate 00:00:00  (ie midnight)

@todate must be @todate 23:59:59 (ie last second of the day).

0
Comment
Question by:RossEdwards
[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
5 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 2000 total points
ID: 24755699
1. This should help

update ur_table
set ur_datetime_col = dateadd(d, datediff(d, 0, ur_datetime_col), 0)

2. This should help

select * from ur_table
where ur_datetime_col between ur_datetime_col = dateadd(d, datediff(d, 0, ur_datetime_col), 0)
and ur_datetime_col = dateadd( ms, -2 , dateadd(d, datediff(d, 0, ur_datetime_col), 1))
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24755701
UPDATE urTable
SET urDateColumn = CONVERT(Varchar, urdatecolumn, 112)
WHERE urDateColumn >=@fromdate AND urDateColumn < @todate -1

UPDATE urTable
SET urDateColumn = DATEADD(second, -1, @Todate )
WHERE urDateColumn >= @todate -1  AND urDateColumn < @Todate
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24755712
Minor mistake for 2:

select * from ur_table
where ur_datetime_col between dateadd(d, datediff(d, 0, ur_datetime_col), 0)
and dateadd( ms, -2 , dateadd(d, datediff(d, 0, ur_datetime_col), 1))
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24755726
For 2, if you want it for the current day, then below should help

select * from ur_table
where ur_datetime_col between dateadd(d, datediff(d, 0, getdate()), 0)
and dateadd( ms, -2 , dateadd(d, datediff(d, 0, getdate()), 1))

or Otherwise subtract the days from getdate()
0
 
LVL 1

Author Closing Comment

by:RossEdwards
ID: 31598820
Many thanks this worked!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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