SQL Server 日期格式 MM/DD/YYYY [英] SQL Server date format MM/DD/YYYY

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

问题描述

如何在 SQL Server 中检查日期字符串是否为 MM/DD/YYYY 格式?

How do I check if a date string is in the MM/DD/YYYY format in SQL Server?

推荐答案

SET DATEFORMAT MDY;
SELECT CASE WHEN ISDATE(@string) = 1 
  AND @string LIKE '[0-1][0-9]/[0-3][0-9]/[1-2][0-9][0-9][0-9]' 
  THEN 1 ELSE 0 END;

如果结果为 1,则它是一个有效日期,但不能保证它就是用户想要的日期.如果他们输入:

If the result is 1, it's a valid date, but there's no guarantee that it's the date the user meant. If they enter:

06/07/2012

无法知道它们是指 6 月 7 日还是 7 月 6 日.最好的办法是让用户从下拉列表或日历控件中选择日期,这样您就可以控制格式并避免任何不必要的解释.您的应用层可以使用强类型变量/参数并插入到正确类型的列中.

There is no way to know if they meant June 7 or July 6. Your best bet is to make users pick dates from drop-downs or calendar controls, which allows you to control the format and avoid any needless interpretation. Your application layer can use strongly typed variables / parameters and insert into properly typed columns.

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

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