如何将特定字符/字符串格式化为datetime?

时间:2017-03-28 17:15:34

标签: sql sql-server-2008 datetime

下面的字符串/ char是SQL Server不支持的日期格式,我需要在SQL Server 2008中将其转换为datetime数据类型

20170202131759

此日期格式包括年,月,日,小时,分钟,秒

4 个答案:

答案 0 :(得分:1)

select cast(stuff(stuff(stuff('20170202131759', 9, 0, ' '), 12, 0, ':'), 15, 0, ':') as datetime) 是一种可解析的格式,可让您的字符串看起来像&投:

Ext.application({
    name: 'My Window',

    launch: function () {

        Ext.define('MyWindow', {
            extend: 'Ext.window.Window',
            title: 'My Window'//,
            // renderTo: Ext.getBody()
        });

        Ext.define('MyForm', {
            extend: 'Ext.form.Panel',
            bodyPadding: 5,
            width: 350,

            // The form will submit an AJAX request to this URL when submitted
            url: '',

            // Fields will be arranged vertically, stretched to full width
            layout: 'anchor',
            defaults: {
                anchor: '100%'
            },

            // The fields
            defaultType: 'textfield',
            items: [{
                fieldLabel: 'First Name',
                name: 'first',
                allowBlank: false
            }, {
                fieldLabel: 'Last Name',
                name: 'last',
                allowBlank: false
            }],

            // Reset and Submit buttons
            buttons: [{
                text: 'Reset',
                handler: function () {
                    this.up('form').getForm().reset();
                }
            }, {
                text: 'Submit',
                formBind: true, //only enabled once the form is valid
                disabled: true,
                handler: function () {
                    var form = this.up('form').getForm();
                    if (form.isValid()) {
                        form.submit({
                            success: function (form, action) {
                                Ext.Msg.alert('Success', action.result.msg);
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.msg);
                            }
                        });
                    }
                }
            }]
        });

        var myWindow = new MyWindow();
        var myForm = new MyForm();

        myWindow.items.add(myForm);
        myWindow.show();
        // var myForm = new MyForm();

    }
});

(假设为mmdd)

答案 1 :(得分:0)

SQL Server无法将此字符串隐式转换为DateTime,您需要插入一些字符:

Select SUBSTRING('20170202131759',1,4) + '-' + SUBSTRING('20170202131759',5,2) + '-' + SUBSTRING('20170202131759',7,2) + ' ' + SUBSTRING('20170202131759',9,2)+ ':' + SUBSTRING('20170202131759',11,2) + ':' + SUBSTRING('20170202131759',13,2)

会给你这个:

2017-02-02 13:17:59,然后它可能是DateTime字符串

答案 2 :(得分:0)

如果您遇到的格式不是其中一个可用于转换的内置格式,那么您通常必须构建"构建"之一。

这可以通过单独的SUBSTRING()函数调用或使用STUFF()函数将其按到一种格式(以及CAST()实际调用的enter image description here调用来轻松完成a DATETIME):

-- This will grab each section, append necessary separators and cast to a DATETIME
SELECT CAST(STUFF(STUFF(STUFF('20170202131759',13,0,':'),11,0,':'),9,0,' ') AS DATETIME)

示例

fiddle

答案 3 :(得分:0)

这使用left()substring()构建以下字符串格式yyyyMMdd HH:mm:ss,并使用datetime将其转换为convert()数据类型。

declare @str varchar(15)= '20170202131759';

select convert(datetime,
               left(@str,8)+' '
               +substring(@str,9,2)+':'
               +substring(@str,11,2)+':'
               +substring(@str,13,2)
               )

rextester演示:http://rextester.com/HEJ3250