Discover datediff function in sql, include the articles, news, trends, analysis and practical advice about datediff function in sql on alibabacloud.com
= mktime ($ hour, $ min, $ sec, $ month, $ day, $ year );Return $ ret;}/***** Simulate the datediff function in sqlserver *******$ Part type: StringValue Range: year, month, day, hour, Min, SecIndicates the part of the date to be added.$ Date1, $ date2 type: TimestampTwo dates to be comparedReturn type: Numeric Value* ************** End *(*************/Function
= mktime ($ hour, $ min, $ sec, $ month, $ day, $ year );Return $ ret;}/***** Simulate the datediff function in sqlserver *******$ Part type: stringValue Range: year, month, day, hour, min, secIndicates the part of the date to be added.$ Date1, $ date2 type: timestampTwo dates to be comparedReturn type: Numeric Value* ************** End *(*************/Function
--getdate Current system date, timeSelect getdate()--2014-11-24 13:22:40.407--the CONVERT () function can display date/time data in different formatsSelect Convert(varchar( -),getdate(), at)--2014-11-24--DateDiff Returns the number of date and time boundaries across two specified datesSelect DateDiff( Day,'2014-11-24','2014-12-28')-- theSelect
GetDate ()
2.DateDiff (' s ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 514,592 seconds
DateDiff (' d ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 5 days
3.DatePart (' W ', ' 2005-7-25 22:56:32 ') returns a value of 2 i.e. Monday (Sunday is 1, Saturday is 7)
DatePart (' d ', ' 2005-7-25 22:56:32 ') returns a value of 25, or 25th
DatePart (' y ', ' 2005-7-25 22:56:32 '
SQL Time function, the need for friends can refer to the next.
1. Current system date, time select GETDATE ()2. DateAdd returns a new datetime value based on adding a period of time to the specified date for example: Add 2 days to the date select DATEADD (day,2, ' 2004-10-15 ')--return: 2004-10-17 00:00:00.000For example: Query current time last three days contents in descending order select * from table w
')
Select how many weeks of the year =datename (week, ' 2004-10-15 ')
, today is the week =datename (weekday, ' 2004-10-15 ')
function parameters/Functions
GetDate () returns the current date and time of the system
DateDiff (INTERVAL,DATE1,DATE2) returns the difference between Date2 and date1 two dates, as specified by interval date2-date1
DATEADD (interval,number,date) in the manner specified by interval,
You can understand that by using simple DATEDIFF and DATEADD functions, you can find many different dates that may be meaningful.
All the examples so far only calculate the number of time intervals between the current time and "", and then add it
The date is calculated based on the time interval of "1900-01-01. Assume that you modify the number of time intervals or use different time intervals to call
The DATEADD
index from the beginning of the first, parameter three is to delete several, parameter four is the inserted stringSelect STUFF (' 123abc ', 4,3, ' hello ')--substring to intercept the specified length from the specified string--Parameter one is the original string, parameter two is to intercept the beginning of the index of the string, parameter three is the length of interceptionSelect SUBSTRING (' ABCDEFG ', 3, 1)--Time-date function--system consta
Label:
------------------date is converted into a month and a day and seconds--------------- Select ' R ' +convert (varchar), GETDATE (), 112)+right (CAST (Power (10,2) as varchar) +datepart (hour, GETDATE ()), 2)+right (CAST (Power (10,2) as varchar) +datepart (minute, GETDATE ()), 2)+right (CAST (Power (10,2) as varchar) +datepart (SS, GETDATE ()), 2)+right (CAST (Power (10,3) as varchar) +datepart (MS, GETDATE ()), 3) SQL Server t
random date within the specified interval, but we want to use the date and time of the two functions: DateAdd and DateDiff. Use DateDiff to calculate the interval value of the date interval, the same idea we calculated a random integer value, and then through the DATEADD to get our expectation of a random date. The T-SQL code for the encapsulated
Tags: 0.00 interval based on different str diff system article arithmetic operationsOne, SQL Server time Date function detailed 1. Current system date, time Select GETDATE () 2. DateAdd returns a new datetime value based on adding a period of time to the specified date Example: Add 2 days to the date Select DATEADD (day,2, ' 2004-10-15 ')--return: 2004-10-17 00:00:00.000 3.
above function is as follows:
SELECT dbo.ufn_RandNum(, ) AS RandNum, dbo.ufn_RandNum(, ) AS RandNum; GO
The query result after execution is as follows:
Generate any random date in the date range
We will continue to explain how to obtain any random date in the specified range, but we need to use two functions of date and time: DATEADD and DATEDIFF. Use DATEDIFF
(M,0,getdate ()) +1, 0))--. this MondaySELECT DATEADD (wk, DATEDIFF (Wk,0,getdate ()), 0) --. this SundaySelect DATEADD (Wk,datediff (Wk,0,getdate ()), 6) --. first day of the yearSELECT DATEADD (yy, DATEDIFF (Yy,0,getdate ()), 0) --. the last day of the yearSELECT DateAdd (Ms,-3,dateadd (yy, DATEDIFF (Yy,0,getdate ()
Label:SQL Server time Date function detailed, SQL Server, time date,1. Current system date, timeSelect GETDATE ()2. DateAdd returns a new datetime value based on adding a period of time to the specified dateExample: Add 2 days to the dateSelect DATEADD (day,2, ' 2004-10-15 ')--return: 2004-10-17 00:00:00.0003. DateDiff returns the number of date and time boundari
returned when the argument to the MONTH () function is an integer, that is, SQL Server considers itsis January 1900.· Year ()The year () function syntax is as follows:Year (The year () function returns the years value in Date_expression.Reminder: When using a date function,
Server DATEPART () function returns part of SQL Server datetime field.
The syntax for the SQL Server DATEPART () function is:DATEPART (Portion, datetime)where datetime is a SQL Server datetime field and part of the name is one of the following: Ms for millisecondsYy for ye
function | function/*
T-sql:17 a date-time related Custom function (UDF), Sunday as the last day of the week, not affected by the @ @DateFirst, language version
are collected or refined from the old articles!
Tips:
(@ @Datefirst + datepart (weekday, @Date))% 7 judgment Week is the most insured! Independent of @ @DateFi
a view
DROP VIEW [dbo].[View name]
GO
---------------
--determine if the name of the function to be created exists
IF exists(SELECT * fromDbo.sysobjectsWHEREId= object_id(N'[dbo]. [function name]') andXtypeinch(N'FN'N'IF'N'TF'))
--Delete a function
DROP FUNCTION [dbo].[Name of f
The window function, like an aggregate function, is an aggregate calculation of the set of rows. It is used to define a window for a row (where the window refers to the collection of rows to be manipulated), which operates on a set of values, does not need to group the data using the GROUP BY clause, and can return both the column and the aggregate column of the underlying row in the same row. Anyway, I und
value of the Fetch fieldSelect DATEPART (mi,createtime) from Life_unite_product--Take the minute value of the Time fieldSelect DATEPART (ss,createtime) from Life_unite_product--takes the seconds value of the Time fieldSelect DATEPART (ms,createtime) from Life_unite_product--take the millisecond value of the Time fieldSelect DATEADD (yy,-1,createtime) from life_unite_product----fetch Time field (year is reduced by 1)Select DATEADD (mm,3,createtime) from life_unite_product----fetch Time field (mo
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.