SQL server obtains the records of the current day or month.

Source: Internet
Author: User

Jsp adds a date to the database

MS SQL SERVER:

NSERT into student (studentid, time1) values ('15', getdate ());

MY SQL

Insert into tablename (fieldname) values (now ())

Obtains the number of all rows of a table in the database.

Connection conn = DbConnection. connectToDb ();

Statement stat = conn. createStatement ();

ResultSet rs1_stat.exe cuteQuery ("select count (*) from book ");

Rs. next ();

Rs. getInt (1); // it seems that you have tried rs. getInt (0) before );

SQL reads the records of the current day or month

SQLSQL Server

SQL reads the records of the current day or month

When I work overtime this evening and compare the time in the database with that of the current day, it is definitely not feasible. Because the time format in the table is as follows: 16:50:08. 050. If we compare the time directly with the time of the current day, we will never get accurate data, but we can [format] the time in this format into, that is, only the year-month-day, then, format the date in the format of year-month-day.

In this way, the idea is coming out!

We need to use the Convert () function to format the date. We need to use three parameters. First, we need to format the date of the day, Convert (varchar (10), getDate (), 120)

In this way, we can format the date of the day to 2007-2-2, and then format the date in the database table.

Convert (varchar (10), TimeFiled, 120). Finally, we can use an SQL statement to get the data of the day.

For example:

Program code

Select * From VIEW_CountBill Where Convert (varchar (10), [time], 120) = Convert (varchar (10), getDate (), 120)

Note:

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 to be converted. Here is [time]. the last one is the format. This value is optional: 20 or 120. It complies with [ODBC specifications] and the input/output style is yyyy-mm-dd hh: mm: ss [. fff]

For details, refer to SQL Server's online help!

T-SQL queries the records of the current month in the table

Idea: Use the Month () function to retrieve the Month in the time field to be searched, and then retrieve the Month of the current Month. The comparison is OK.

Example:

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.