BigInt无法转换为正确的日期格式

时间:2018-10-11 18:55:24

标签: sql sql-server tsql

所以我有一个查询,我正在尝试写两行具有可变结果的列。一个是日期,一个是时间。我的查询看起来像

Select Schedule ID , Job_Name , next_run_date , next_run_time

这些值将根据我运行的数据库而有所不同。例如,[next_run_date]可能= 20181014,[next_run_time]可能读为1000,即凌晨1点。但是,如果我在其他服务器上运行它,则它可能具有完全不同的一组值,但格式相同。

我尝试使用

将列转换为日期/时间格式,但未成功
CONVERT(varchar(10),CONVERT(date,[next_run_date],110),110) AS 'Next Run' 

并得到“ 不允许从数据类型int到日期的显式转换

我想要显示的是[next_run_date] might = 10-14-2018[next_run_time] = 01:00。只是不确定如何正确转换。我不必必须将privs写入数据库。如果我正确地阅读(至少对于日期列而言),我将不得不从Bigin转换为Varchar并转换为ToDate,但不清楚如何完整地编写。

4 个答案:

答案 0 :(得分:2)

对于时间字段,您可以在其中填充Option Explicit Public Sub Yarislar() Dim s As String, html As HTMLDocument Set html = New HTMLDocument With New XMLHTTP60 .Open "GET", "https://yenibeygir.com/at/15673/budakhan", False .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8" .send s = .responseText End With Dim hTable As HTMLTable, clipboard As Object html.body.innerHTML = s Set hTable = html.querySelector(".at_Yarislar") Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") clipboard.SetText hTable.outerHTML clipboard.PutInClipboard ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial End Sub
以及上午10点以下时间的格式。

日期部分可以用2次强制转换和CONVERTFORMAT完成。

从INT到VARCHAR的日期,格式为“ mm-dd-yyyy”:

Public Sub test()
    Dim s As String, html As HTMLDocument, hTable As Long, hTables As Object, clipboard As Object, ws As Object
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With New XMLHTTP60
        .Open "POST", "https://yenibeygir.com/jokey/updatestats", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send "id=10294&LastYear=True"
        s = .responseText
    End With

    Set html = New HTMLDocument

    html.body.innerHTML = s
    Set hTables = html.querySelectorAll(".Stats")

    For hTable = 0 To hTables.Length - 1
        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        clipboard.SetText hTables.item(hTable).outerHTML
        clipboard.PutInClipboard
        ThisWorkbook.Worksheets("Sheet1").Cells(GetLastRow(ws, 1) + 2, 1).PasteSpecial
    Next
End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
    With ws
        GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
    End With
End Function

从INT到'hh:mi'格式的VARCHAR的时间:

Dim clipboard As DataObject
Set clipboard = New DataObject
clipboard.SetText hTable.outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial

示例代码段

:

返回:

CONVERT(VARCHAR(10), CAST(CAST([next_run_date] AS VARCHAR(8)) AS DATE), 110)

答案 1 :(得分:0)

选择convert(datetime,cast(20181014 as varchar),102)

注意:

  

CAST是ANSI-SQL规范的一部分;而CONVERT不是。   实际上,CONVERT是SQL实现特定的。转换差异   在于它接受一个可选的style参数,该参数用于   格式。

答案 2 :(得分:0)

您需要先将bigint转换为varchar,然后转换为日期:

CONVERT(date,CONVERT(varchar(10),[next_run_date]),110) AS 'Next Run'

答案 3 :(得分:0)

您还可以将数字分成几个部分,并设计日期和时间。

DECLARE @Date INT=20181014
DECLARE @Time INT=123456

SELECT CONVERT(DATE,
    SUBSTRING(CONVERT(VARCHAR(20),@Date),1,4)+'/'+
    SUBSTRING(CONVERT(VARCHAR(20),@Date),5,2)+'/'+
    SUBSTRING(CONVERT(VARCHAR(20),@Date),7,2)
) AS [Date]

SELECT CONVERT(TIME,
    SUBSTRING(CONVERT(VARCHAR(20),@Time),1,LEN(CONVERT(VARCHAR(20),@Time))-4)+':'+
    SUBSTRING(CONVERT(VARCHAR(20),@Time),LEN(CONVERT(VARCHAR(20),@Time))-3,2)+':'+
    SUBSTRING(CONVERT(VARCHAR(20),@Time),LEN(CONVERT(VARCHAR(20),@Time))-1,2)
) AS [Time]