Tsql VarChar 隐式转换为 SmallDateTime

发布于 2024-07-12 08:25:42 字数 1049 浏览 8 评论 0 原文

当对返回的结果使用smalldatetime约束执行以下命令时,我得到零结果:

Execute sp_proc @DateOfBirth =   '01/01/1900' 

Execute sp_proc @DateOfBirth =   '1900-01-01' 

但是当使用以下varchar参数时,我突然得到了与2000年1月1日相对应的结果。 Smalldatetime 隐式转换不适用于 mm/dd/YYYY 日期,仅适用于 mm/dd/YY 日期?

Execute sp_proc @DateOfBirth =   '01/01/00' 

这将返回 01/01/2000 生日的所有结果!

CREATE PROCEDURE [dbo].[sp_proc] 
    -- Add the parameters for the stored procedure here

    @DateOfBirth SmallDateTime = null 

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT *
    FROM
        view_People
    WHERE
        FirstName LIKE '%' + IsNull(@FName ,FirstName) + '%'
    AND
        LastName LIKE '%' + IsNull(@LName,LastName) + '%'
    AND
        DOB = IsNull(@DateOfBirth,DOB) 
    AND
        SSN = IsNull(@SSN,SSN)

When executing the following using a smalldatetime constraint on the returned results, I get zero results:

Execute sp_proc @DateOfBirth =   '01/01/1900' 

or

Execute sp_proc @DateOfBirth =   '1900-01-01' 

But when using the following varchar argument all the sudden i get results which correspond to 1 Jan 2000. Does smalldatetime implicit conversion not work for mm/dd/YYYY dates and only mm/dd/YY dates?

Execute sp_proc @DateOfBirth =   '01/01/00' 

This returns all results for 01/01/2000 birthdays!

CREATE PROCEDURE [dbo].[sp_proc] 
    -- Add the parameters for the stored procedure here

    @DateOfBirth SmallDateTime = null 

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT *
    FROM
        view_People
    WHERE
        FirstName LIKE '%' + IsNull(@FName ,FirstName) + '%'
    AND
        LastName LIKE '%' + IsNull(@LName,LastName) + '%'
    AND
        DOB = IsNull(@DateOfBirth,DOB) 
    AND
        SSN = IsNull(@SSN,SSN)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜点 2024-07-19 08:25:42

“01/01/00”基于“ 进行转换两位数年份截止”选项。
所以它变成 01 Jan 2000。

如果您发送“1900-01-01”,那么您的代码显示只有当您的 DOB = 01 Jan 1900 时您才会获得结果。

除此之外,它的工作方式如广告所示。
除非您想运行执行 sp_proc @DateOfBirth = DEFAULT 来强制忽略 DOB

"01/01/00" is converted based on the "two digit year cutoff" option.
So it becomes 01 Jan 2000.

If you send '1900-01-01', then your code shows you'll only get results if you have DOB = 01 Jan 1900.

Other than that, it's working as advertised.
Unless you want to run Execute sp_proc @DateOfBirth = DEFAULT to force DOB to be ignored

虚拟世界 2024-07-19 08:25:42

从技术上讲,您应该将以下输出进行比较:

Execute sp_proc @DateOfBirth =   '01/01/00'

与:

Execute sp_proc @DateOfBirth =   '01/01/2000'

Execute sp_proc @DateOfBirth =   '2000-01-01'

Technically, you should be comparing the output of:

Execute sp_proc @DateOfBirth =   '01/01/00'

to:

Execute sp_proc @DateOfBirth =   '01/01/2000'

or

Execute sp_proc @DateOfBirth =   '2000-01-01'
北凤男飞 2024-07-19 08:25:42

使用安全格式,如 YYYYMMDD(无破折号)

运行这些命令看看会发生什么

select convert(smalldatetime,'19000101')
select convert(smalldatetime,'01/01/00')
select convert(smalldatetime,0)

use a safe format like YYYYMMDD (no dashes)

run these also to see what happens

select convert(smalldatetime,'19000101')
select convert(smalldatetime,'01/01/00')
select convert(smalldatetime,0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文