在SQL Server中的date数据类型列中插入日期

时间:2017-09-29 04:04:49

标签: sql sql-server string-to-datetime

如何将格式为dd-mm-yyyy的日期插入数据类型Date的列中?

尝试以下但显示错误

INSERT INTO Profiles (first_name, last_name, email, phone, 
                      [city(hometown)], created_at, gender, 
                      referral_code,marital_status) 
VALUES ('anusha', 'pariti', 'anusha.pariti@gmail.com', '8105987404', 
        'Bangalore', '16-04-2015', 'F', '7L5FZW', 'Y'),
       ('Ashish', 'Singh', 'ashish.singh@gmail.com', '9876890463', 
        'Bangalore','23-05-2015', 'M', 'KF34MF', 'Y')

错误:

  

从字符串转换日期和/或时间时转换失败。

INSERT INTO Profiles (first_name, last_name, email, phone, 
                      [city(hometown)],created_at, gender, 
                      referral_code, marital_status) 
VALUES ('anusha', 'pariti', 'anusha.pariti@gmail.com', '8105987404', 
        'Bangalore', CAST('16-04-2015' AS DATE), 'F', '7L5FZW', 'Y'),
       ('Ashish', 'Singh', 'ashish.singh@gmail.com', '9876890463', 
        'Bangalore', CAST('23-05-2015' AS DATE), 'M', 'KF34MF', 'Y')

错误:

  

从字符串转换日期和/或时间时转换失败。

INSERT INTO Profiles (first_name, last_name, email, phone, 
                      [city(hometown)], created_at, gender,  
                      referral_code, marital_status) 
VALUES ('anusha', 'pariti', 'anusha.pariti@gmail.com', '8105987404', 
        'Bangalore', CONVERT(DATE, '16-04-2015'), 'F', '7L5FZW', 'Y'),
       ('Ashish', 'Singh', 'ashish.singh@gmail.com', '9876890463', 
        'Bangalore', CONVERT(DATE, '23-05-2015'), 'M', 'KF34MF', 'Y')

错误:

  

从字符串转换日期和/或时间时转换失败。

5 个答案:

答案 0 :(得分:1)

尝试使用可接受的格式插入日期,例如使用20150416代替'16-04-2015

INSERT INTO Profiles (first_name, last_name, email, phone, [city(hometown)],
    created_at, gender, referral_code,marital_status)
VALUES
    ('anusha', 'pariti', 'anusha.pariti@gmail.com', '8105987404', 'Bangalore',
     '20150416', 'F', '7L5FZW', 'Y'),
    ('Ashish', 'Singh', 'ashish.singh@gmail.com', '9876890463', 'Bangalore',
     '20150523', 'M', 'KF34MF', 'Y');

这将允许您将数据存储为created_at列中的日期。这绝对是存储日期信息的方法。如果您以后希望以原始方式格式化 created_at列,则可以使用CONVERT,例如

SELECT
    CONVERT(varchar, created_at, 105) AS created_at
FROM Profiles;

答案 1 :(得分:1)

SQL Server中最安全的日期文字是YYYYMMDD

E.g。 INSERT into ... Values('20170523',...

您似乎习惯了DD-MM-YYYY,如果您想使用该序列,则需要使用带有样式编号的转换。

convert(date,'23 -05-2017',120)

答案 2 :(得分:0)

在SQL Server 2012+中,您可以使用

Select try_convert(date, '16-04-2015', 105)

否则你必须正确格式化字符串

答案 3 :(得分:0)

您是否尝试过YYYY-MM-DD格式? SQL中的数据格式为YYYY-MM-DD。

答案 4 :(得分:0)

你可以试试这个。

dd-mm-yyyy是意大利格式,样式代码是105,这就是您应该使用此代码进行转换的原因。

INSERT INTO Profiles (first_name, last_name, email, phone, 
                      [city(hometown)], created_at, gender,  
                      referral_code, marital_status) 
VALUES ('anusha', 'pariti', 'anusha.pariti@gmail.com', '8105987404', 
        'Bangalore', CONVERT(DATE, '16-04-2015',105), 'F', '7L5FZW', 'Y'),
       ('Ashish', 'Singh', 'ashish.singh@gmail.com', '9876890463', 
        'Bangalore', CONVERT(DATE, '23-05-2015',105), 'M', 'KF34MF', 'Y')