简体   繁体   English

将SQL Server varchar转换为日期时间以进行算术运算

[英]Converting SQL Server varchar to datetime for arithmetic

I want to combine two separate columns into date time and subtract 20 hours in SQL Server. 我想将两个单独的列合并为日期时间,并在SQL Server中减去20小时。

This works in Oracle: 这在Oracle中有效:

create table t ( x varchar2(8),y varchar2(6));

insert into t values ('20151106','090000');

select to_char(to_date(x||' '||y, 'yyyymmdd hh24miss')
             - (20/24),'dd-mm-yyyy hh24:mi:ss') as "date minus 20 hrs" from t;

date minus 20 hrs
- -----------------
05-11-2015 13:00:00

I can't crack it in SQL Server, I can get parts to work but can't combine the two dates (too many functions). 我不能在SQL Server中破解它,我可以使零件工作,但不能将两个日期结合在一起(功能太多)。

This works: 这有效:

CREATE TABLE t (x VARCHAR(8), y VARCHAR(6))

INSERT INTO t
VALUES('20151105', '150800')

SELECT  
    DATEADD(HOUR, -20, CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2), ':', SUBSTRING(y, 5, 2)) AS TIME))
FROM t

This works: 这有效:

SELECT DATEADD(HOUR, -20, x)
FROM t

But I can't combine them: 但是我不能将它们结合在一起:

SELECT 
    DATEADD(HOUR, -20,
            CONCAT(SUBSTRING(x, 1, 4), '-', SUBSTRING(x, 5, 2), '-',
                   SUBSTRING(x, 7, 2), ' ',
                   CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2),
                               ':', SUBSTRING(y, 5, 2)) AS TIME)))
FROM t

The string you're passing to datepart evaluates to: 您传递给datepart的字符串的计算结果为:

select dateadd(hour, -20, '2015-11-05T15:08:00.0000000')

But you'll get a Conversion failed error for more than 3 zeroes. 但是您会收到3个以上的零Conversion failed错误。 An easy way to fix that is to remove the second column's conversion to time . 一种简单的解决方法是删除第二列到time的转换。 While you're at it, you could use + instead of concat , which is easier to read: 在使用它时,可以使用+代替concat ,这更容易阅读:

select dateadd(hour, -20, 
            substring(x,1,4) + '-' +
            substring(x,5,2) + '-' + 
            substring(x,7,2) + 'T' + 
            substring(y,1,2) + ':' + 
            substring(y,3,2) + ':' +
            substring(y,5,2))
from t

See it working at SE Data. 看到它在SE Data上的运作。

If you CONVERT the values properly to a datetime , there should be no issue with what you're trying to do. 如果您CONVERT正确的价值观的datetime ,应该有与你正在试图做什么都没问题。 You want to give SQL Server a date in the following format, or one of the other acceptable formats: 您要为SQL Server提供以下格式的日期,或其他可接受格式之一:

YYYYMMDD HH:MM:SS    

So applying that to your sample, the date portion is fine, but you need to get the time format correct before you can convert it to a valid date: 因此,将其应用到样本中,日期部分就可以了,但是您需要先正确设置时间格式,然后才能将其转换为有效日期:

CREATE TABLE #t ( x VARCHAR(8), y VARCHAR(6) )
INSERT  INTO #t
VALUES  ( '20151105', '150800' )
-- we want 20151105 15:08:00
SELECT  DATEADD(HOUR, -20,
                ( CONVERT(DATETIME, x + ' ' + 
                            LEFT(y, 2) + ':' + 
                            SUBSTRING(y, 3, 2) + ':' + 
                            RIGHT(y, 2)) )) AS Result
FROM    #t

DROP TABLE #t

Produces 产生

2015-11-04 19:08:00.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM