简体   繁体   English

SQL Server日期查询

[英]SQL Server date query

I am new to development and want to know the professional way to deal with dates in SQL Server. 我是开发新手,想了解SQL Server中处理日期的专业方法。 In my applications mainly we deal with the DATE datatype and no concern with time part. 在我的应用程序中,主要处理DATE数据类型,而不关心时间部分。 Also maintaining the format dd/mm/yyyy 同时保持格式dd / mm / yyyy

So for example if I have the table with the following structure. 因此,例如,如果我的表具有以下结构。

EmployeeTable  
---------------
emp_id int  
emp_name varchar(50)  
join_date date  

and if I want to query "join_date" in between start date and end date and pass the dd/mm/yyyy as stored procedure criteria and want to query. 如果我要在开始日期和结束日期之间查询“ join_date”,并通过dd / mm / yyyy作为存储过程标准,并要查询。

What is the professional way to handle dates? 处理日期的专业方法是什么? I always convert date in varchar and then do the comparison which I guess is the unprofessional way of doing it. 我总是将日期转换为varchar,然后进行比较,我猜这是不专业的方式。 So please guide how to do it in procedure with example I would appreciate. 因此,请以示例的方式指导我如何进行操作。

SQL handles dates just fine, so you do not need to convert the dates. SQL处理日期就很好,因此您不需要转换日期。

If you pass in the parameters as date types, then you will have no problem: 如果您将参数作为date类型传递,那么您将没有问题:

CREATE PROCEDURE myProc 
    @start date, 
    @end date 
AS 
    SELECT emp_id, emp_name, join_date
    FROM EmployeeTable
    WHERE join_date BETWEEN start AND end;

Unless you want to format a date in your output in a specific way, there's no reason to convert the date to a varchar . 除非您要以特定方式格式化日期,否则没有理由将日期转换为varchar You're using the date datatype, so let SQL do the comparisons for you. 您正在使用date数据类型,因此让SQL为您进行比较。

If you want to compare dates in a date range, you can use this: 如果要比较日期范围内的日期,可以使用以下方法:

WHERE join_date BETWEEN '2010-01-01' AND '2010-12-31'

Keep dates as dates. 将日期保留为日期。 Do not convert it to strings. 不要将其转换为字符串。 That is unnecessary. 那是没有必要的。 When you send dates in to SQL Server from your code, do it with parameters, then you don't have to worry about the right format in your strings. 当您从代码中将日期发送到SQL Server时,只需使用参数即可完成操作,则不必担心字符串中格式的正确性。

SQL Server Date data types: SQL Server日期数据类型:

  • Date: 0001-01-01 through 9999-12-31 日期:0001-01-01至9999-12-31
  • SmallDateTime: 1900-01-01 through 2079-06-06 (Accuracy 1 minute) SmallDateTime:1900年1月1日至2079-06-06年(精度1分钟)
  • DateTime: January 1, 1753, through December 31, 9999 (Accuracy millisecond) 日期时间:1753年1月1日至9999年12月31日(精度为毫秒)
  • DateTime2: 0001-01-01 through 9999-12-31 (Accuracy 100 nanoseconds) DateTime2:0001-01-01至9999-12-31(精度为100纳秒)

It's a minor point but worth noting that all queries presented to SQL Server are in TEXT. 这是次要的一点,但值得注意的是,提供给SQL Server的所有查询都在TEXT中。 At some stage, based on some language and translation setting in the data access layer (OLEDB, Native, ADO) it gets turned into a textual form, so dates are always presented as "text". 在某个阶段,根据数据访问层(OLEDB,Native,ADO)中的某种语言和翻译设置,它会变成文本形式,因此日期始终以“文本”形式显示。

The best format to use is always YYYYMMDD for SQL Server. 对于SQL Server,最好使用的格式始终是YYYYMMDD。 Even YYYY-MM-DD can be wrong, for obscure dateformat settings. 由于模糊的dateformat设置,即使YYYY-MM-DD也可能是错误的。 See this example. 请参阅此示例。

set dateformat dmy  -- more than common for non-US locations
select CONVERT(varchar, convert(datetime, '2010-12-31'), 112)

It fails . 它失败了

Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

That covers the format to use when you have to construct the date embedded in the SQL statement. 这涵盖了您必须构造SQL语句中嵌入的日期时使用的格式。 When possible however, please parameterize queries for benefits like 但是,如果可能,请对查询进行参数化以获取诸如

  1. prevention of SQL injection 防止SQL注入
  2. letting the db connectivity layer ensure the right formats when generating the TSQL 让数据库连接层确保生成TSQL时的正确格式
  3. query plan re-use on the SQL Server 查询计划在SQL Server上重用
  4. point 3 = better performing queries and more efficient SQL Server 第3点=性能更好的查询和更高效的SQL Server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM