SQL Server的期待不同的日期格式 [英] SQL Server expecting different date format

查看:117
本文介绍了SQL Server的期待不同的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了SQL Server的默认语言为英国。下面的正确返回23 ...

  sp_configure的'默认语言'

我还设置我登录的语言英国太有...

  sp_defaultlanguage @loginame ='Login123',@language ='英国'

然而,当我尝试指定 13/12/2015 从ASP经典的日期时间的存储过程的参数,我得到...


  

错误的数据类型为nvarchar转换为日期时间。


从Management Studio中,用正确的登录,它的工作原理。

为什么ASP经典有这样的效果吗?我是pretty确保它使用了正确的连接字符串。

编辑:

它不是连接字符串。不知怎的,日期的格式被发送到数据库之前正确转换。我可以从探查看到这一点。


解决方案

使用 转换 风格:


  

英国/法国


  
  

103 = DD / MM / YYYY


  SELECT CONVERT(DATETIME,'13 / 12/2015」,103); - 与存储过程
DECLARE @d DATETIME = CONVERT(DATETIME,'13 / 12/2015」,103);。EXEC [DBO] [my_stored_procedure] @d;



另一种方法是使用 ISO 8601 日期文字是文化的独立的:


  

<强>在使用ISO 8601格式的优点是,它是一个国际标准。此外,由使用该格式指定的日期时间值是毫不含糊的。同时,这种格式不受由SET DATEFORMAT或SET LANGUAGE设置。


  
  

YYYY-MM-DDTHH:MM:SS [.mmm]


  EXEC [DBO] [my_stored_procedure]。'2015-12-13T00:00:00'

I have set the SQL Server's default language to "British". The following correctly returns 23...

sp_configure 'default language'

I have also set the language of my login to "British" too with...

 sp_defaultlanguage @loginame = 'Login123', @language = 'British'

However, when I attempt to specify 13/12/2015 as a DateTime stored procedure parameter from ASP Classic, I get...

Error converting data type nvarchar to datetime.

From Management Studio, with the correct login, it works.

Why would ASP Classic have this effect? I'm pretty sure it's using the correct connection string.

EDIT:

Its not the connection string. Somehow the date's format is being converted incorrectly before being sent to the database. I can see this from Profiler.

解决方案

Use CONVERT with style:

British/French

103 = dd/mm/yyyy

SELECT CONVERT(DATETIME, '13/12/2015', 103);

-- with stored procedure
DECLARE @d DATETIME = CONVERT(DATETIME, '13/12/2015', 103);

EXEC [dbo].[my_stored_procedure] @d;


Another method is to use ISO 8601 date literal which is culture independent:

The advantage in using the ISO 8601 format is that it is an international standard. Also, datetime values that are specified by using this format are unambiguous. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE settings.

yyyy-mm-ddThh:mm:ss[.mmm]

EXEC [dbo].[my_stored_procedure] '2015-12-13T00:00:00'

这篇关于SQL Server的期待不同的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆