SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateTime Quesiton
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 07/18/2006 :  09:52:26  Show Profile  Click to see raysefo's MSN Messenger address  Reply with Quote
Hi,

i have a datetime variable , lets say;

@date = 2006-07-18 16:51:13

and i wanna put zero to the seconds part of it, like this below;

@date = 2006-07-18 16:51:00

how can i do it?

thanks

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 07/18/2006 :  09:57:18  Show Profile  Reply with Quote

declare @date datetime
set @date = '20060718 16:51:13'

select @date as Date, dateadd(second, -datepart(second, @date), @date) as RoundedDownToMinute

/*results
Date                                                   RoundedDownToMinute                                    
------------------------------------------------------ ------------------------------------------------------ 
2006-07-18 16:51:13.000                                2006-07-18 16:51:00.000
*/


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

Q
Yak Posting Veteran

Netherlands
76 Posts

Posted - 07/18/2006 :  10:00:03  Show Profile  Reply with Quote
@dateZero = dateadd(second, -1*datepart(second, @date), @date)

Edited by - Q on 07/18/2006 10:00:58
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 07/18/2006 :  11:33:32  Show Profile  Reply with Quote
The solutions already posted do not remove the milliseconds part of a datetime.

You can use the F_START_OF_MINUTE function in this link to do what you want:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64755


It does it this way:
select dateadd(ms,-(datepart(ss,@date)*1000)-datepart(ms,@date),@date)



CODO ERGO SUM
Go to Top of Page

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 07/19/2006 :  07:33:05  Show Profile  Reply with Quote
Good point, Michael


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 07/19/2006 :  07:38:16  Show Profile  Reply with Quote
Another variation

declare @date datetime
set @date = '20060718 16:51:13.45'

select	dateadd(minute, datepart(minute, @date), dateadd(hour, datediff(hour, 0, @date), 0))

/* RESULT

	2006-07-18 16:51:00.000
*/



KH

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000