【问题标题】:How to get SQL Server Date in its native format?如何以本机格式获取 SQL Server 日期?
【发布时间】:2010-12-02 15:24:59
【问题描述】:

我需要从 SQL Server 数据库 (SQL Server 2000) 中获取日期,日期是这样存储的:

2009-09-30 00:00:00.000

我想将此日期传递给经典 ASP 函数以在数据库中使用此日期 在其他地方,但是当我得到日期时,它会在我的 ASP 代码中转换为依赖于区域设置的版本,例如:

30/09/2009

我只希望日期按原样格式化日期,因此它再次正确似乎没有必要,尽管如果这是唯一的方法那么很好 - 有没有办法将它视为字符串数据,所以它保持这样,所以它可以在同一日期插入回数据库,而不需要转换?

【问题讨论】:

    标签: sql sql-server date asp-classic sql-server-2000


    【解决方案1】:

    以下是完整的方法列表:http://www.sql-server-helper.com/tips/date-formats.aspx

    请注意,存储实际上是两个整数。第一个是 1900 年 1 月 1 日之前或之后的天数,另一个是自午夜以来的毫秒数。

    【讨论】:

    • 该日期格式列表很方便,总是需要以某种方式处理日期 - 这对将来的参考很有用。
    【解决方案2】:

    日期不存储为“2009-09-30 00:00:00.000”,它存储为一个 8 字节数字,其中前 4 个字节是自 1900 年 1 月 1 日以来的天数,其他 4 个字节是毫秒日期。

    要获得您的格式,请使用 convert(varchar, dt, 121)。要获取原始格式,请使用 convert(binary(8), dt)

    编辑:编辑您的问题后,您似乎真正想要的是能够执行转换日期 -> 字符串 -> 日期。

    为此,您可以使用 @s = convert(varchar, @dt, 121); @dt = 转换(日期时间,@s,121)。只要在两个方向上始终如一地应用,所有其他格式也可能会起作用。

    【讨论】:

    • 我知道 SQL 以自己的方式存储日期,但不知道查询分析器中正在进行隐式转换,应该检查实际格式是什么。
    • 我认为这是我所说的本机格式,而不是实际的本机格式 - 但是我正在寻找一种复制日期的方法,并认为在 SQL 中使用本机格式会更好 - 但由于无论如何日期都在转换,我会转换它。
    • 确实,我在函数之间传递了日期 - 想在我的问题中具体说明,但应该提到这就是我想要的,将其标记为答案。
    【解决方案3】:

    这是一个函数...

    '**********************************************************************************************************
    '' @SDESCRIPTION:   Gets an independent sql representation for a datetime string standardised by ISO 8601 
    '' @DESCRIPTION:    If you want to create a sql statement containing a date query in the where clause,
    ''                  use this function to create a datetime object.
    ''                  EXAMPLE:
    ''                  In Sql Server: Declare @timeTable TABLE (name int, starttime datetime)
    ''                  In VBScript: sql = "SELECT * FROM timeTable WHERE starttime = " & date.toMsSqlDateFormat(cDate('2007-01-01 15:30:00'))
    ''                  Results in: SELECT * FROM timeTable WHERE starttime = cast('2006-01-01T15:30:00' as datetime)
    ''                  NOTE: only for MS SQL Server
    '' @PARAM:          dat [date]: the date/time you want to cast
    '' @RETURN:         [string] the formatted date string
    '**********************************************************************************************************
    public function toMsSqlDateFormat(dat)
        if dat = empty then exit function
        toMsSqlDateFormat = "cast('" & year(dat) & "-" & lib.custom.forceZeros(month(dat), 2) & "-" & _
                            padLeft(day(dat), 2, "0") & "T" & padLeft(hour(dat), 2, "0") & ":" & _
                            padLeft(minute(dat), 2, "0") & ":" & padLeft(second(dat), 2, "0") & "' as datetime)"
    end function
    
    '******************************************************************************************************************
    '' @SDESCRIPTION:   right-aligns a given value by padding left a given character to a totalsize
    '' @DESCRIPTION:    example: input: <em>22</em> -> output: <em>00022</em> (padded to total length of 5 with the paddingchar 0)
    '' @PARAM:          value [string]: the value which should be aligned right
    '' @PARAM:          totalLength [string]: whats the total Length of the result string
    '' @PARAM:          paddingChar [string]: the char which is taken for padding
    '' @RETURN:         [string] right aligned string.
    '******************************************************************************************************************
    public function padLeft(value, totalLength, paddingChar)
        padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
    end function
    
    public function clone(byVal str, n)
        for i = 1 to n : clone = clone & str : next
    end function
    

    【讨论】:

      【解决方案4】:

      如果您只想检索日期并随后在另一个查询中使用它,最好不要将其转换为字符串。这样您就不必担心在转换过程中使用了哪些格式。

      为此,您需要使用参数化查询。你可以用谷歌搜索,但在带有 VB 的经典 ASP 中,它看起来像:

      ' Retrieval
      ...
      Set objCommand = CreateObject("ADODB.Command")
      ...
      objCommand.CommandText = "SELECT SomeDate FROM SomeTable"
      ...
      Set objRS = objCommand.Execute
      ...
      dtSomeDate = objRS("SomeDate").Value ' This is a Date object
      
      
      ' Write date to db
      ...
      Set objCommand = CreateObject("ADODB.Command")
      ...
      objCommand.CommandText = "INSERT INTO SomeTable (...,SomeDate) VALUES (...,?)"
      Set objParam = objCommand.CreateParameter("SomeDate", adDateTime, adParamInput, dtSomeDate)
      ...
      objCommand.Execute
      

      【讨论】:

      • 谢谢,我使用了转换,但是在 SQL 方法之间传递日期可能对某些人或其他人有用。使用 CreateParameter 来保留日期格式是我忘记了你可以用参数化查询来做的事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多