替换SQL列值

时间:2014-01-08 10:38:36

标签: sql sql-server-2008

我有一个表tbltimetable,有三列Appdatebegintimeendtime(所有datetime数据类型)。

示例数据:

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

我需要如下的最终结果:从appdate替换begintime和endtime中的日期。

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

我尝试从各个列中分割日期和时间,但现在我在开始时间和结束时间中追加/替换appdate中的值。

select convert(date,tbltimetable.AppDate) as [date], 
convert(varchar(8), convert(time, tblAppTime.AppDate)) as [Time]

6 个答案:

答案 0 :(得分:2)

我只使用DATEADD / DATEDIFF模式两次:

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

这可以避免分割出单独的组件或转换为字符串或从字符串转换。

内部DATEDIFF(day,begintime,AppDate)只计算begintimeAppDate之间的午夜转换次数 - 它不会注意任何一个的时间成分,并返回一个整数。如果我们在DATEADD上添加(begintime)该天数,我们会在datetime的同一天从逻辑上获得AppDate值,但其时间成分与{begintime不变1}}。

答案 1 :(得分: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
;

使用 DatePart() 功能从begintimeendtime抓取时间部分,然后使用<{>} {}添加到原始appdate DateAdd() 功能。

答案 2 :(得分:1)

不是这么简单吗

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 ║
╚═════════════════════════╩═════════════════════════╩═════════════════════════╝

<强>查询

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

结果集

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

答案 3 :(得分:0)

使用DATEPART函数获取日期时间的每个组件。 例如:

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)

通过这种方式,您可以提取每个组件,然后以所需格式形成日期。

您可以创建自己的功能以仅获取DATE部分。 检查以下功能:

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

测试此新功能的示例代码:

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

通过这种方式,您也可以编写时间函数!

答案 4 :(得分:0)

尝试这个......

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

答案 5 :(得分:0)

只需添加这些值:)您可以将时间部分转换为TIME数据类型,但它可以在不转换的情况下工作。