SQL Server Functions and stored procedures, SQL Server Stored Procedures
The SQL Server function is a structure that encapsulates one or more SQL statements.SQL Server functions are divided into system
.exists Find out that the CategoryID in the category table are not equal to N.categoryid C.categoryid,c.[name] First query statement: Select C.categoryid,c.[name] from Category C left join News N in C.categoryid=n.categoryid where N.categoryid is Null Second query statement: Select Categoryid,[name] from category where NOT exists ( SELECT * FROM News where Category.categoryid=news.categoryid ) * The first performance is undoubtedly the best when the CategoryID is not repeated in the news table,
, getdate), 0)
13. Last day of the yearSelect dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate () + 1, 0 )).
Date and time functions in SQL Server1. Current System Date and TimeSelect getdate ()
2. dateadd returns a new datetime value based on a period of time added to the specified date.For example, add
() function of php () functions are much more efficient, but we cannot ignore the execution overhead of mysql databases. Therefore, FROM_UNIXTIME () is fast without considering the database overhead.
PHP source code reference:
The Code is as follows:
Copy code
Header ("Content-type: text/html; charset = UTF-8 ");// Program running time$ Starttime = explode ('', microtime ());/* · The following code area ·········*/$ Link = m
Use of datepart () Functions* The datepart () function allows you to conveniently retrieve all parts of the period.* Date: 18:15:36. 513* YY: 2006 of the year* Mm: 7 months* DD: 2 days in the month* DY: 183 days in the year* WK: week 27 in the year* DW: The day in the week.* QQ: Quarter 3 of the year* Hh: 18 hours* Mi: 15 minutes* Ss: 36 seconds* The following simple statements demonstrate the obtained resu
To get the day of the week, you need to use the DATE function in SQL Server: Datename ().Today is the day of the week, Example 1: Set Language N ' 中文版 ' select Datename (Weekday, GETDATE ()) WednesdayToday is the day of the week, Example 2: Set Language N ' Simplified Chinese ' select Datename (Weekday, GETDATE ()) WednesdayToday is the day of the week, example 3: Set Datefirst 1select datepart (W
compared to the row function. Of course, the column functions are extended in some SQL products, and other column functions for special purposes are defined.2. Use of common functionsAfter some basic knowledge of the column functions, let's introduce some of the common functions
SQL DateWhen we work on dates, the hardest task is to ensure that the date you insert is formatted to match the format of the date column in the database.As long as the data contains only the date parts, running the query will not be a problem. However, if time is involved, the situation is a little more complicated.Be
Transferred from: http://www.cnblogs.com/zhangq723/archive/2011/02/16/1956152.htmlOne, SQL Server date-time functionsDate and time functions in SQL Server1. Current system date, timeSelect GETDATE ()2. DateAdd returns a new datetime value based on adding a period of time to
Http://www.w3school.com.cn/sql/sql_dates.aspSQL DateWhen we work on dates, the hardest task is to ensure that the date you insert is formatted to match the format of the date column in the database.As long as the data contains only the date parts, running the query will not be a problem. However, if time is involved, t
the X field type SQL>SelectMax (distinct Sal), Max (XM) fromTable3; Max (distinctsal) max (XM)---------------- --------5555.55zhumin ([Distinct|all]x) The maximum value of the row x column is selected in the function statistics table. The "parameter" all means the maximum value for all values, distinct only the maximum value for the different values, the default is all if there is a parameter distinct or all, a space is separated from the X (column).
Single-record functions in SQL
1. ASCIIReturns the decimal number corresponding to the specified character;
SQL> select ASCII ('A') A, ASCII ('A') A, ASCII ('0') zero, ASCII ('') space from dual;
A A zero space
------------------------------------
65 97 48 32
2. CHRReturns the corresponding characters;
SQL> select CHR
In this lesson, we will learn how to use the built-in SQL System functions to complete the query job. SQL System functions are commonly used, including string functions, date functions,
CONVERT (varchar), GETDATE (), 121): 2006-05-16 10:57:49.700
Select CONVERT (varchar), GETDATE (), 126): 2006-05-16t10:57:49.827
Select CONVERT (varchar), GETDATE (), ():?????????? 1427 10:57:49:907am
In SQL database, there is a class of functions that have to be mentioned, that is, SQL Server datetime function, which is the most common
Let's take a look at common date functions.Date functions:1. Last month: for example, the last month is 201201 :( CONVERT ([varchar] (6), dateadd (month, (-1), getdate (), (112 )))2, yesterday: 2012-02-02 CONVERT (varchar (100), GETDATE ()-1, 23)3. Today: 2012-02-03 CONVERT (varchar (100), GETDATE (), 23)4, last day of last month: 23:59:59. 997 dateadd (MS,-3, DATEADD (mm, DATEDIFF (mm, 0, getdate (), 0
SQL functions for format conversionConvert (nvarchar (12), @ date, 111 ):
The first parameter represents the length to be intercepted, the second parameter represents the database field, and the last parameter represents the date and time format to be intercepted, as shown in the following table:
.htm
Now use this knowledge to solve my problems, the first is the daily Statistics reported, accurate to the date limit: convert (varchar (one), Reporttime, 20) that is YYYY-MM-DD
The group by problem is then resolved with the SQL code:
Copy Code code as follows:
Select Reportperson,convert (varchar, reporttime,) as ' Reportime ', COUNT (*) as reporttotal from Pcr_constructinfo Where (repo
date to the last day of the month. Please note: This example and the other examples in this article will only use the DateDiff and DATEADD functions to calculate the date we want. Each example will get the date you want to calculate by calculating the previous time interval and then adding and reducing it. This is
Let's take a look at some of these functions.
Convert in SQL
Format:
CONVERT (Data_type,expression[,style])
Description
This style is generally in the type of time (datetime,smalldatetime) and String type (Nchar,nvarchar,char,varchar)When they are converted to each other.
Example:
The code is as follows
Copy Code
SELECT CONVERT (varchar), GETDATE (), () now
The r
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.