如何获取SQL Server日期的原生格式?

问题描述:

我需要从一个SQL Server数据库(SQL Server 2000中)一个日期,该日期存储为这样:如何获取SQL Server日期的原生格式?

2009-09-30 00:00:00.000 

我想通过这个日期到经典ASP函数使用此日期在数据库 别的地方,但是当我得到的日期,它在我的ASP代码转换为语言环境相关的版本,如:

30/09/2009 

我只想日期,是格式化的日期,因此是再次纠正似乎是不必要的,但如果这是唯一的方式那么罚款 - 有什么办法把它作为字符串数据处理它这样的话,所以它可以作为相同的日期插入到数据库中,而不需要转换?

日期不存储为'2009-09-30 00:00:00.000',它存储为8字节的数字,其中前4个字节是从1900年1月1日开始的几天,其他4个字节是毫秒在日期。

要获得您的格式,请使用convert(varchar,dt,121)。要获得原始格式使用转换(二进制(8),DT)

编辑:在你的问题被编辑后,看来你真正想要的是能够执行转换日期 - >字符串 - >日期。为此,您可以使用@s = convert(varchar,@dt,121); @dt = convert(日期时间,@s,121)。所有其他格式都可能会起作用,只要它们在两个方向上一致应用。

+0

我知道SQL以自己的方式存储日期,但不知道查询分析器中是否存在隐式转换,应该检查实际格式是什么。 – RoguePlanetoid 2009-09-30 13:27:29

+0

如果你知道这一点,你是什么意思与本机格式? – erikkallen 2009-09-30 13:51:25

+0

我认为这是我说的原生格式,而不是实际的原生格式 - 但是我正在寻找一种方法来复制日期,并认为使用本机格式在SQL中会更好 - 但由于日期正在转换,无论如何,我将会转换它。 – RoguePlanetoid 2009-09-30 14:27:48

这里有办法的完整列表:http://www.sql-server-helper.com/tips/date-formats.aspx

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

+0

这日期格式列表是很方便的,总是要应对一些这样或那样的日期 - 这应该是未来的参考价值。 – RoguePlanetoid 2009-09-30 13:28:09

下面是该函数...

'********************************************************************************************************** 
'' @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 

如果你只是想检索日期和随后在另一查询中使用它,你离它不转换为字符串要好。这样,您无需担心在转换过程中使用的格式。

为此,您需要使用参数化查询。您可以谷歌这一点,但在传统的ASP与VB它看起来像:

' 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 
+0

谢谢,我已经使用了转换,但在SQL方法之间传递日期可能对某些事情或其他人有用。使用CreateParameter保存日期格式是我忘记了你可以用参数化查询做的事情。 – RoguePlanetoid 2009-10-01 08:49:26