转换日期&时间

时间:2014-01-17 01:28:24

标签: sql sql-server

我有下表: -

enter image description here

我需要将其转换为: -

enter image description here

请注意,TIMEINA的日期必须完全符合TIMEIN1。至于时间,它将被固定,即7.30AM,8.30AM等。

我尝试了以下SQL,但它不起作用: -

enter image description here

由于

3 个答案:

答案 0 :(得分:2)

我遇到的主要问题是该表有多少查询?

如果您在一个报告中执行此复杂逻辑,那么一定要使用SELECT。

但为了更好的解决方案,我迫不及待地想要。

为什么不使用计算列?

由于它是日期且不确定,因此您无法使用持久关键字来物理存储计算值。

但是,您只会在表定义中使用此代码,而不是在每个查询中。

我为前两个范围和两个样本日期项做了案例。其余的由你决定。!

-- Just play
use tempdb;
go

-- Drop table
if object_id('time_clock') > 0
drop table time_clock
go

-- Create table
create table time_clock
(
    tc_id int,
    tc_day char(3),
    tc_time_in datetime,
    tc_time_out datetime,
    tc_division char(3),
    tc_empid char(5),

    -- Use computed column
    tc_time_1 as
    (
    case 

      -- range 1
      when 
        tc_division = 'KEP' and 
        cast(tc_time_in as time) between '04:30:00' and '07:29:59'
      then 
        cast((convert(char(10), tc_time_in, 101) + ' 07:30:00') as datetime)

      -- range 2
      when 
        tc_division = 'KEP' and 
        cast(tc_time_in as time) between '17:30:00' and '19:29:59'
      then 
        cast((convert(char(10), tc_time_in, 101) + ' 19:30:00') as datetime)

      -- no match
      else NULL
    end
    ) 
);

-- Load store products
insert into time_clock values
(1,'SUN', '20131201 06:53:57', '20131201 16:23:54', 'KEP', 'A007'),
(2,'TUE', '20131201 18:32:42', '20131201 03:00:47', 'KEP', 'A007');

-- Show the data
select * from time_clock

预期结果。

enter image description here

答案 1 :(得分:1)

您应该将代码发布为代码而不是图像。

在任何情况下,您的代码都会将datetimetime值进行比较。只是进行转换。而不是timein1 between . . .,请使用:

cast(timein1 as time) between . . .

编辑:

哦,你还需要得到完整的约会。为此,请在datetime上使用算术:

cast('07:30:00' as datetime) + cast(cast(timein1 as date) as datetime)

cast上的双timein1只是删除时间组件。

答案 2 :(得分:0)

检查此脚本可能会对您有所帮助。

print replace(convert(nvarchar(25) , getdate(), 120), '-', '') -- yyyyMMdd HH:mm:ss
print replace(convert(nvarchar(25) , getdate(), 102), '.', '') -- yyyyMMdd
print convert(nvarchar(25) , getdate(), 108) -- HH:mm:ss

HERE是T-SQL

SELECT  [Day],
        TIMEIN1,
        TIMEOUT1,
        DIVISION,
        EMPLOYEE,

        --IF TIMEIN1 is varchar
        LEFT(TIMEIN1, 9) +
        CASE
            WHEN DIVISION = 'KEP' AND RIGHT(TIMEIN1, 8) BETWEEN '04:30:00' AND '07:29:59'
                THEN '07:30:00'
            WHEN DIVISION = 'KEP' AND RIGHT(TIMEIN1, 8) BETWEEN '17:30:00' AND '19:29:59'
                THEN '19:30:00'
            WHEN DIVISION = 'SER' AND RIGHT(TIMEIN1, 8) BETWEEN '04:30:00' AND '08:29:59'
                THEN '08:30:00'
            WHEN DIVISION = 'SER' AND RIGHT(TIMEIN1, 8) BETWEEN '17:30:00' AND '20:29:59'
                THEN '20:30:00'
            ELSE '00:00:00'
        END AS TIMEINA


        --IF TIMEIN1 is Date Time
        , replace(convert(nvarchar(25) , TIMEIN1, 102), '.', '') + ' ' +
        CASE
            WHEN DIVISION = 'KEP' AND convert(nvarchar(25) , TIMEIN1, 108) BETWEEN '04:30:00' AND '07:29:59'
                THEN '07:30:00'
            WHEN DIVISION = 'KEP' AND convert(nvarchar(25) , TIMEIN1, 108) BETWEEN '17:30:00' AND '19:29:59'
                THEN '19:30:00'
            WHEN DIVISION = 'SER' AND convert(nvarchar(25) , TIMEIN1, 108) BETWEEN '04:30:00' AND '08:29:59'
                THEN '08:30:00'
            WHEN DIVISION = 'SER' AND convert(nvarchar(25) , TIMEIN1, 108) BETWEEN '17:30:00' AND '20:29:59'
                THEN '20:30:00'
            ELSE '00:00:00'
        END AS TIMEINA

FROM    tempdb.dbo.TSA

check this for more info