1

I have a table tbltimetable, with three columns, Appdate, begintime, endtime (all datetime datatype).

Sample data:

Appdate                   begintime                   endtime
2013-09-11 00:00:00.000   1900-01-01 12:30:00.000     1900-01-01 14:45:00.000

I need a final result like the following: replace the date in begintime and endtime from appdate.

Appdate                   begintime                 endtime
2013-09-11 00:00:00.000   2013-09-11 12:30:00.000   2013-09-11 14:45:00.000

I tried to split the date and time from respective columns, but now I 'm struck in appending /replacing the values from appdate in begintime and endtime.

select convert(date,tbltimetable.AppDate) as [date], 
convert(varchar(8), convert(time, tblAppTime.AppDate)) as [Time]
2
  • what you have tried till no please add that query here Commented Jan 8, 2014 at 10:47
  • 1
    @James S, He can't help when company decides to use SQL 2008. Commented Jan 8, 2014 at 11:00

6 Answers 6

2

I just use the DATEADD/DATEDIFF pattern twice:

declare @t table (Appdate datetime,begintime datetime,endtime datetime)
insert into @t(Appdate,begintime,endtime) values
('2013-09-11T00:00:00.000','1900-01-01T12:30:00.000','1900-01-01T14:45:00.000')

select Appdate,
    DATEADD(day,DATEDIFF(day,begintime,AppDate),beginTime) as AdjustedBegin,
    DATEADD(day,DATEDIFF(day,endtime,AppDate),endtime) as AdjustedEnd
from @t

This avoids mucking about with splitting out separate components or converting to/from strings.

The inner DATEDIFF(day,begintime,AppDate) just computes the number of midnight transitions between begintime and AppDate - it doesn't pay any heed to the time components of either, and it returns a whole number. If we add (DATEADD) that number of days onto begintime, we logically get a datetime value from the same date as AppDate but with it's time components unchanged from begintime.

Sign up to request clarification or add additional context in comments.

Comments

1
DECLARE @t table (
   appdate   datetime
 , begintime datetime
 , endtime   datetime
);

INSERT INTO @t (appdate, begintime, endtime)
  VALUES ('2013-09-11 00:00:00.000', '1900-01-01 12:30:00.000', '1900-01-01 14:45:00.000')
;

SELECT appdate
     , begintime
     , DateAdd(mi, begintime_mi, DateAdd(hh, begintime_hh, appdate)) As new_begintime
     , endtime
     , DateAdd(mi, endtime_mi, DateAdd(hh, endtime_hh, appdate)) As new_endtime
FROM   (
        SELECT appdate
             , begintime
             , DatePart(hh, begintime) As begintime_hh
             , DatePart(mi, begintime) As begintime_mi
             , endtime
             , DatePart(hh, endtime) As endtime_hh
             , DatePart(mi, endtime) As endtime_mi
        FROM   @t
       ) As finding_time_portions
;

This grabs the portions of time from begintime and endtime using the DatePart() function and then adds them on to the original appdate using the DateAdd() function.

Comments

1

is it not as simple as this

DECLARE @t TABLE(Appdate DATETIME,begintime DATETIME,endtime DATETIME)
INSERT INTO @t  
VALUES
('2013-09-11 00:00:00.000','1900-01-01 12:30:00.000','1900-01-01 14:45:00.000')

SELECT * FROM @t

╔═════════════════════════╦═════════════════════════╦═════════════════════════╗
║         Appdate         ║        begintime        ║         endtime         ║
╠═════════════════════════╬═════════════════════════╬═════════════════════════╣
║ 2013-09-11 00:00:00.000 ║ 1900-01-01 12:30:00.000 ║ 1900-01-01 14:45:00.000 ║
╚═════════════════════════╩═════════════════════════╩═════════════════════════╝

Query

SELECT   Appdate 
       , begintime + Appdate AS  begintime
       , endtime + Appdate   AS  endtime
FROM @t

Result Set

╔═════════════════════════╦═════════════════════════╦═════════════════════════╗
║         Appdate         ║        begintime        ║         endtime         ║
╠═════════════════════════╬═════════════════════════╬═════════════════════════╣
║ 2013-09-11 00:00:00.000 ║ 2013-09-11 12:30:00.000 ║ 2013-09-11 14:45:00.000 ║
╚═════════════════════════╩═════════════════════════╩═════════════════════════╝

Comments

0

TRY this...

select a.Appdate,
 convert(datetime,(convert(varchar(10),convert(date,a.Appdate))+' ' + convert(varchar(10),convert(time,a.begintime)))) begintime,
convert(datetime,(convert(varchar(10),convert(date,a.Appdate))+' ' + convert(varchar(10),convert(time,a.endtime)))) endtime
from tblTimetable a

Comments

0

Use DATEPART function to fetch the each component of the datetime. for Example:

DECLARE @datetime DATETIME
SET @datetime = GETDATE()
SELECT DATEPART(YEAR, @datetime), DATEPART(MONTH, @datetime), DATEPART(DAY, @datetime),  DATEPART(HOUR, @datetime), DATEPART(MINUTE, @datetime), DATEPART(SECOND, @datetime), DATEPART(MILLISECOND, @datetime)

In this way, you can extract each component and then form the dates in the desired format.

You can create your own function to get the DATE part only. Check the below function:

CREATE FUNCTION dbo.GetDateOnly
(
@Datetime DATETIME
)
RETURNS DATE
AS BEGIN

DECLARE @dateString VARCHAR(10), @temp VARCHAR(3)
SET @dateString = CAST(DATEPART(YEAR, @Datetime) AS VARCHAR)
SET @temp = CAST(DATEPART(MONTH, @Datetime) AS VARCHAR)
SET @dateString = @dateString + '-' + REPLICATE('0', 2 - len(@temp)) + @temp
SET @temp = CAST(DATEPART(DAY, @Datetime) AS VARCHAR)
SET @dateString = @dateString + '-' + REPLICATE('0', 2 - len(@temp)) + @temp
RETURN CONVERT(DATE, @dateString)
END
GO

Sample code to test this new function:

SELECT GETDATE()
SELECT dbo.GetDateOnly(GETDATE())

In this way, you can write your function for time also!

2 Comments

i get the way of extracting each part, but my problem is to append them
@gs11111 I have provided new FN for this problem. Let me know If this helps!
0

Just add these values :) You could cast time part to TIME datatype, but it does work without casting either.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.