SQL Server query time tips sharing _mssql2008

Source: Internet
Author: User
Tags sql server query
1. Convert time to String form:
Copy Code code as follows:

CONVERT (varchar, field name,)--------yyyy-mm-ddhh:ss:mm0000
Convert (char (10), field name, YYYY-MM-DD)------

2, if you are using the SQL editor (Microsoft's own tools), the query date does not need to use a function conversion, for example
SQL code
Copy Code code as follows:

Select*frominfowheredatatime> ' 2012-11-1100:00:00 ' anddatetime< ' 2012-12-1200:00:00 '

3, if the use of Hibernate to query the data (using HQL) can not use the above method, because the query is the object, then the parameter passed to ensure that the passed parameter is the date type, or you will be prompted "conversion error"
Java code
Copy Code code as follows:

Frompersonaspwherep.date=:d ate

Parameters passed: Date must be of date type
4, we found that the contents of the database were yyyy-mm-dd00:00:00000 to milliseconds after the date query, but if we use the Calendar class to get the date type, there is no way to be precise to milliseconds, We can only get yyyy-mm-dd00:00:00xxx milliseconds is the millisecond of the system, then the query will have errors
5, time interval segment query
For example, I would like to query the information between 2011-11-11 and 2012-12-12, and its default query time is
2011-11-1100:00:00 to 2012-12-1200:00:00, which means that 2012-12-1223:59:59 seconds are not queried.
Workaround: Start the query 1 seconds ahead of time, one more day (1 seconds less)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.