Discover sql server format function, include the articles, news, trends, analysis and practical advice about sql server format function on alibabacloud.com
The DateDiff () function and the GETDATE () function are needed when you are doing SQL Server development and sometimes need to get the data in the table today, yesterday, this week, last week, this month, and last month.DATEDIFF (DatePart, StartDate, EndDate)Explanation: Calculating the time differenceDatepare Value:
specified times (including the second date but not the first date.
When month is used as the date part, datediff returns the number of the first day of the month between two dates (including the second date but not the first date.
When week is used as the date part, datediff returns the number of Sundays between two dates (including the second but not the first.
Overflow value for a smaller unit of time:
Milliseconds 24 days
Seconds 68 years
Minutes 4083
Others has no overflow res
. Aggregate open Window functionsMany aggregation functions can be used as operations for window functions, such as sum,avg,max,min.The aggregate window function can only be used with the partition by clause or without any statements, and ORDER by cannot be used with the aggregate window function.For example, query the total number of orders and order information for an employeeWith OrderInfo as(SELECT COUNT (OrderID) over (PARTITION by EmployeeID) as
Replace function in SQL Server: Implements a bulk substitution of a string in a field.Note: It is strongly recommended to back up the database before replacing it to avoid catastrophic consequences.Set [Content]=replace ([Content],'www.abc.com',' www. bbb.com') ;Description: Replace the www.abc.com in the Content field with ===> www.bbb.comif the content field
() functionThe ROUND function is used to round a numeric field to a specified number of decimal digits.SQL ROUND () syntaxSELECT ROUND (column_name,decimals) from table_nameParameter descriptioncolumn_name required. The field to be rounded.Decimals required. Specifies the number of decimal digits to return.For example:Select ROUND (15.258, 1) The result is: 15.300You can use the round () function with the
=
@ P
+
Len
(
@ Delimiter
)
-
1
End
Else
Begin
Break
End
End
Declare @ S Nvarchar ( 1000 ) If @ P = 0 And @ I = @ N Begin Set @ S = @ Expression End Else If @ I = @ N Begin Set @ S = Substring ( @ Expression , 1 , @ P - Len ( @ Delimiter )) End Return @ S End Usage: Code
Code highlighting produced by Actipro CodeHighlighter (
identity) column, which is also called an auto-increment column. in SQL Server 2000, the identity column is defined by identity, the following is an example of a function related to getting the id value of the last inserted record
SQL Server, you can use scope_ I Dentity (
Tags: SQL Server recursive function gets the owning Department collectionRecursive format:With CTE name (field list) as (Query Statements )Get all subordinate department statements for the current department:Create function getchildrendepartments (@currentdepartid int)Returns tableAsReturnWith CTE (Id,mainid)As(
Tags: io ar using for data on CTI as database--SQL SERVER determines if there is a trigger, stored procedure--Determine the storage process and delete if it existsIF (EXISTS (SELECT * from sysobjects WHERE name= ' procedurename ' and type= ' P '))DROP PROCEDURE procedurename--Judgment trigger, if present deleteIF (EXISTS (SELECT * from sysobjects WHERE id=object_id (N ' [dbo].[ Triggername] ') and OBJECTPRO
Tags: varchar nbsp rtti arch Color Take string span intercept string SQL ServerRight (field name, N)SQL intercepts the string from the right. n represents the number of bits intercepted from the right.SELECT Right ('123456'4) SELECT Left ('123456'4)--------------------------------------------------------------Give me a chestnut:Declare @starttime as datetimeDeclare @endtime as datetimeSet @starttime = G
If you want to remember the records in a table for random display, or randomly extract a record, we do not need to write a long stringProgramCodeHow can we create a random number and then make a so-called "random record"? You only need to use a small function in SQL Server to implement the random sorting or random extraction
SQL Server Stored Procedure/function encryption/Decryption
Encryption of stored procedures and stored functions: With Encryption
Create procedure DBO. sp_xml_main
@ Table_name nvarchar (260) = '',
@ Dirname nvarchar (20) =''
With Encryption
As
Begin
....................
End
Go
Decryption of stored procedures and stored Functions
If exists (select * From DBO. syso
, how many bytes will it occupy? Try it:DECLARE @Value1 int, @Value2 int set @Value1 = 2 Set @Value2 = 2000000000 Select datalength (@Value1) Select LE N (@Value1) Select datalength (@Value2) select LEN (@Value2)In both cases, the DATALENGTH () function returns 4.Because the int type, regardless of the value, always uses 4 bytes.The LEN () function essentially treats integer values as data that has bee
, @Value2 int set @Value1 = 2 Set @Value2 = 2000000000 Select Datalength (@Value1) Select LEN (@Value1 Select Datalength (@Value2) Select LEN (@Value2) In both cases, the DATALENGTH () function returns 4. Because the int type, regardless of the value, always uses 4 bytes. The LEN () function essentially treats integer values as data that has been converted to character, so in this case, it returns 1 and 10,
') returns a value of 206 that is the No. 206 Day of the Year DatePart (' yyyy ', ' 2005-7-25 22:56:32 ') returns a value of 2005 that is 2005 Use the CONVERT function in SQL Server to handle date strings: For example, a beginner might write in a format like "2008-2-27": [HTML]View Plain copy print?
Select C
The SQL Server2005 database comes with a sort function row_number (),The list is:Syntax: Row_number (): Over (clause)1. Use the Row_number () function to number:Select Row_number () over (order by Sage) ID, * from StudentPrinciple: Sort by sage first, and then number each piece of data.Note: When using the over window functio
--sql Regular Replace function
The Code is as follows: CREATE function Dbo.regexreplace ( @source ntext, the original string @regexp varchar (1000),--Regular expression @rep Lace varchar (1000),--Replaces the value @globalReplace bit = 1, whether it is global replace @ignoreCase bit = 0-Ignore case ) ReturnS varcha R (1000) as begin Declare @hr integ
SET QUOTED_IDENTIFIER ONIdentifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marksSET QUOTED_IDENTIFIER OFFIdentifiers are not quoted and must comply with all Transact-SQL identifier rules.SET NOCOUNT On-Returns the number of rows affectedSET Datefirst 1 SET ANSI_NULLS onAllow comparison operators to return TRUE or FALSE when compared to null values If ColumnA contains a null value, a comparison
Tags: row_numberTransferred from: http://www.maomao365.com/?p=4942The function of the **row_number function in the database is to generate a number for each row according to a certain rule, we often use this property, the table paging operation, the following we will describe the use of the Row_number function to delete the table repeating data row * */*建表*/creat
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.