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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 datetime add datetime
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dalibor
Starting Member

Czech Republic
21 Posts

Posted - 12/17/2012 :  10:42:47  Show Profile  Reply with Quote
Hello,

I have this problem:

DECLARE @hhmmss datetime
DECLARE @dt datetime

SET @hhmmss = {ts '1899-12-31 11:10:00.000'}
SET @dt = {ts '2012-03-12 00:00:00.000'}


select dateadd(s,datediff(s,0,convert(datetime,@hhmmss)),@dt)
select @dt + @hhmmss

--In both cases result is:
2012-03-11 11:10:00.000



I dont understand, why 2012-03-11?
why is not the result 2012-03-12?

I need date(@dt) + time(@hhmmss).
This is 2012-03-12 11:10:00.000.

Please help.

Dalibor

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/17/2012 :  10:54:39  Show Profile  Reply with Quote
This is because unlike for example Excel where the reference date is 12/30/1899, the reference date in SQL Server is 1/1/1900. So use
SET @hhmmss = {ts '1900-01-01 11:10:00.000'}
Go to Top of Page

dalibor
Starting Member

Czech Republic
21 Posts

Posted - 12/17/2012 :  12:16:45  Show Profile  Reply with Quote
I have MS SQL Server 2008 R2 Express, but i need solution for SQL server 2005.
Go to Top of Page

dalibor
Starting Member

Czech Republic
21 Posts

Posted - 12/17/2012 :  12:36:19  Show Profile  Reply with Quote
Yes, i have this solution:

DECLARE @hhmmss datetime
DECLARE @dt datetime
DECLARE @newDatetime datetime
SET @hhmmss = {ts '1899-12-31 11:10:00.000'}
SET @dt = {ts '2012-03-12 00:00:00.000'}

DECLARE @Hour int
DECLARE @Min int

DECLARE @shhmm varchar(20)
SET @Hour = DATEPART(HOUR,@hhmmss)
SET @Min = DATEPART(MINUTE,@hhmmss)

SET @newDatetime = DATEADD(HOUR,@Hour,@dt)
SET @newDatetime = DATEADD(MINUTE,@Min,@newDatetime)
SELECT @newDatetime
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22742 Posts

Posted - 12/24/2012 :  07:54:07  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also you don't need ODBC notations


DECLARE @hhmmss datetime
DECLARE @dt datetime

SET @hhmmss = '11:10:00.000'
SET @dt = '2012-03-12 00:00:00.000'


select dateadd(s,datediff(s,0,convert(datetime,@hhmmss)),@dt)
select @dt + @hhmmss



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 12/24/2012 :  15:34:45  Show Profile  Reply with Quote
quote:
Originally posted by dalibor

Hello,

I have this problem:

DECLARE @hhmmss datetime
DECLARE @dt datetime

SET @hhmmss = {ts '1899-12-31 11:10:00.000'}
SET @dt = {ts '2012-03-12 00:00:00.000'}


select dateadd(s,datediff(s,0,convert(datetime,@hhmmss)),@dt)
select @dt + @hhmmss

--In both cases result is:
2012-03-11 11:10:00.000



I dont understand, why 2012-03-11?
why is not the result 2012-03-12?

I need date(@dt) + time(@hhmmss).
This is 2012-03-12 11:10:00.000.

Please help.

Dalibor


see this to understand how dates are internally stored in sql server

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.05 seconds. Powered By: Snitz Forums 2000