SQL Server takes records for the day or month

Source: Internet
Author: User
Tags comparison getdate

JSP adds a date to the database

MS SQL SERVER:

Nsert into student (studentid,time1) VALUES (' A ', getdate ());

My SQL

INSERT INTO TableName (fieldname) VALUES (now ())

Get all rows of a table in a database

Connection Conn=dbconnection.connecttodb ();

Statement stat=conn.createstatement ();

ResultSet rs=stat.executequery ("SELECT count (*) from book");

Rs.next ();

Rs.getint (1);//It seems to have tried before (0);

SQL takes records for the day or month

Sqlsql Server

SQL takes records for the day or month

This evening overtime, encounter to the database in time and the day time to compare the problem, direct comparison certainly is not. Because the time format in the table is this: 2007-02-02 16:50:08.050, if the direct and the day of the time comparison, it is not always accurate data, but we can the format of the time [format] into 2007-02-02, that is, only the year-month-day, Then the day's time is also formatted as an adult-month-day format.

In this way, the idea came out!

We format the date to use the Convert () function, to use 3 parameters, first to format the date of the day, Convert (varchar (), getDate (), 120)

So we can format the day's date as: 2007-2-2, and then format the date in the database table

Convert (varchar), timefiled,120, and finally we can get the data for the day with an SQL statement.

For example:

Program code

Select * from View_countbill Where Convert (varchar), [time],120) = CONVERT (varchar (), getDate (), 120)

Attention:

The meaning of each parameter in the Convert () function, the first parameter, varchar (10) is the data type provided by the target system, including bigint and sql_variant. User-defined data types cannot be used. The second parameter is the field you want to convert, and I am here [time]. The last one is the format, the value is optional: 20 or 120 can be, it follows the [ODBC specification], the input/output style is: Yyyy-mm-dd HH:MM:SS[.FFF]

Specifically, you can refer to the online Help for SQL Server!

The record for the month in the T-SQL lookup table

Train of thought: the Time field will be looked up with the month () function to take out the month, and then the month of the current month, the comparison is OK

Cases:

Program code

Select * from View_countbill Where Month ([time]) = Month (GetDate ())

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.