SQL stored Procedure statement splicing the time type of the sorrow person

Source: Internet
Author: User

Background: There is a small need to be based on the incoming start and end time, not one hours to count the average data, I thought of the use of stored procedures to achieve.

When SQL is finished, it executes,

  
Hey ~ How, I did not convert the type of place Ah, is the statement splicing error.

The following is a definition of two time variable formats that are in datetime format,

  

  

The problem is in the time-type statement stitching, think of two solutions,

When stitching, the time is changed to the varchar type, or the original time type is defined as the varchar type directly.

Cast () function for type conversion
  

After the change is completed, one executes

  

The data has, but a bit wrong ah, I added the test data is one hours a piece of data ah, how there is a row is 0.
After a closer look, there is a problem finding the query time. (Is this the default time format for SQL?)

  

The two-time data format used for querying in a stored procedure is only accurate to the minute, and the test data that I add myself has a continuous number of hours of data that is 59 minutes in number of seconds, resulting in the computation
Calculate the data for the hour that was supposed to be in the last one hours (I'm glad you added the data to help yourself find a hidden danger AH).

Workaround:

Select CONVERT (varchar), GETDATE (), 25): 2006-05-16 10:57:47.250

Modify the time format.

SQL stored Procedure statement splicing the time type of the sorrow person

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.