DATETIME在SQL Server中给出没有时间的日期

时间:2018-08-02 09:58:04

标签: sql sql-server date insert

我有一个SQL查询,应该转换为datetime。我以不同的方式尝试过它,但是在每一个方面都有问题。

INSERT INTO SETTLEMENT_WIN (COUNTRY,
                            COMMODITY,
                            MARKET_PLACE,
                            START_TIME,
                            END_TIME)
VALUES ('BE', 'EL', NULL, CONVERT(datetime, '2015-12-14 15:45', 'YYYY-MM-DD HH24:MI'), CONVERT(datetime, '2015-12-14 16:00', 'YYYY-MM-DD HH24:MI'));

给出错误:

  

消息8116,级别16,状态1,第1行参数数据类型varchar为   对于转换函数的参数3无效。

INSERT INTO SETTLEMENT_WIN (COUNTRY,
                            COMMODITY,
                            MARKET_PLACE,
                            START_TIME,
                            END_TIME)
VALUES ('BE', 'EL', NULL, CONVERT(datetime, '2008-12-14 15:45', 120), CONVERT(datetime, '2015-12-14 16:00', 120));

插入行,但是在START_TIMEEND_TIME中只有日期,而没有时间

enter image description here

然后,我尝试仅插入时间,但即使如此,也只插入了日期(不允许更改日期格式)。

Declare @Date1 datetime ='2008-12-14 15:45';
Declare @Date2 datetime ='2015-12-14 16:00';

INSERT INTO SETTLEMENT_WIN (COUNTRY,
                            COMMODITY,
                            MARKET_PLACE,
                            START_TIME,
                            END_TIME)
VALUES ('BE', 'EL', NULL, CONVERT(datetime, @Date1, 108), CONVERT(datetime, @Date2, 108));

我在做什么错?我正在使用Microsoft SQL Server Management Studio 17。

我的表的CREATE语句是:

CREATE TABLE [SETTLEMENT_WIN] ([SW_ID] [numeric](18, 0) IDENTITY(1, 1) NOT NULL,
                               [COUNTRY] [varchar](32) NOT NULL,
                               [COMMODITY] [varchar](32) NOT NULL,
                               [MARKET_PLACE] [varchar](32),
                               [START_TIME] [date] NOT NULL,
                               [END_TIME] [date] NOT NULL,
                               CONSTRAINT [SW_PK]
                                   PRIMARY KEY ([SW_ID]));

2 个答案:

答案 0 :(得分:4)

正是我所怀疑的:

[START_TIME] [date] NOT NULL,
[END_TIME] [date] NOT NULL,

如果为datetime数据类型提供date值,则该值的时间部分将丢失。试试:

DECLARE @d date;
SET @d = '2018-08-02T11:15:59.462'
SELECT @d;

请注意,它返回2018-08-02date就是这样,日期。需要使用datetimedatetime2datetimeoffset来存储日期时间。您需要修复表格以解决此问题:

ALTER TABLE dbo.SETTLEMENT_WIN ALTER COLUMN [START_TIME] datetime2(0) NOT NULL;
ALTER TABLE dbo.SETTLEMENT_WIN ALTER COLUMN [END_TIME] datetime2(0) NOT NULL;

然后,您可以在表格中插入日期和时间值。

答案 1 :(得分:1)

@SilverFullbuster,您需要将START_TIME和END_TIME中的数据类型列从日期更改为[datetime]。

来自:

CREATE TABLE [SETTLEMENT_WIN] ([SW_ID] [numeric](18, 0) IDENTITY(1, 1) NOT NULL,
                               [COUNTRY] [varchar](32) NOT NULL,
                               [COMMODITY] [varchar](32) NOT NULL,
                               [MARKET_PLACE] [varchar](32),
                               [START_TIME] [date] NOT NULL,
                               [END_TIME] [date] NOT NULL,
                               CONSTRAINT [SW_PK]
                                   PRIMARY KEY ([SW_ID]));

CREATE TABLE [SETTLEMENT_WIN] ([SW_ID] [numeric](18, 0) IDENTITY(1, 1) NOT NULL,
                               [COUNTRY] [varchar](32) NOT NULL,
                               [COMMODITY] [varchar](32) NOT NULL,
                               [MARKET_PLACE] [varchar](32),
                               [START_TIME] [datetime] NOT NULL,
                               [END_TIME] [datetime] NOT NULL,
                               CONSTRAINT [SW_PK]
                                   PRIMARY KEY ([SW_ID]));

,您可以通过从datetime列中进行提取来提取开始时间/结束时间,如下所示:

SELECT 
    SW_ID,  
    COUNTRY,    
    COMMODITY,  
    MARKET_PLACE,
    START_TIME=convert(varchar(8), START_TIME, 108),
    END_TIME=convert(varchar(8), END_TIME, 108)
FROM
    SETTLEMENT_WIN

结果将是这样的:

enter image description here