SQL Server->> time functions: EOMONTH, Datefromparts, Timefromparts, Datetimefromparts, Datetimeoffsetfromparts

Source: Internet
Author: User

The above functions are all new time functions in SQL Server 2012.

EOMONTH

Returns the month end date of the incoming time, the return data type is date

SELECT EOMONTH (GETDATE())

Result is

 . -  on -  to

Datefromparts

As in C # or Java declares a DateTime instance by passing in year, month, the number of day is worth a DateTime instance. The same is true here. Get a date by passing in the date. But if you fail to construct a valid time for the incoming parameter, you will get an error.

DECLARE @Year int,@Month int,@Day intSET @Year =  -SET @Month =  GenevaSET @Day =  -SELECTDatefromparts (@Year,@Month,@Day) asMyDate

The result is

289  Level  - 1  A  Someofvalues not valid.

If it is legal

DECLARE @Year int,@Month int,@Day intSET @Year =  -SET @Month =  GenevaSET @Day =  -SELECTDatefromparts (@Year,@Month,@Day) asMyDate

That's

 - -  Geneva -  -

Timefromparts

Similar to Datefromparts, except that the incoming hour, MINUTE, Second,millisecond, and millisecond exact digits, and then returns a time type. It is important to note that the fifth argument of this function is that the integer variable is not supported, and must be the display constant passed in.

Like what

DECLARE @Hour int,@Minutes int,@Seconds int,@FractionsOfASecond intSET @Hour =  theSET @Minutes =  atSET @Seconds =  -SET @FractionsOfASecond =  -SELECTTimefromparts (@Hour,@Minutes,@Seconds,@FractionsOfASecond,3) asMyTime

Results

£ º47.500

What if I pass in a null value?

DECLARE @Hour int,@Minutes int,@Seconds int,@FractionsOfASecond intSET @Hour =  theSET @Minutes =  atSET @Seconds =  -SET @FractionsOfASecond =  -SELECTTimefromparts (@Hour,@Minutes,@Seconds,NULL,3) asMyTime

The result is also null

Datetimefromparts

This is a combination of the previous two. The characteristic is that the passed null value is the result becomes null, the illegal value is an error. It is strange that it does not have the exact number of digits of the timefromparts parameter.

DECLARE @Year int,@Month int,@Day int,@Hour intDECLARE @Minutes int,@Seconds int,@MilliSeconds intSET @Year =  -SET @Month =  -SET @Day =  atSET @Hour =  -SET @Minutes =  -SET @Seconds =  theSET @MilliSeconds = 0SELECTDatetimefromparts (@Year,@Month,@Day,@Hour,@Minutes,@Seconds,@MilliSeconds) asMydatetime

Results

 - -  - -  at £ º49.000

Datetimeoffsetfromparts

This is more interesting. Joined the TIMEZONE.

DECLARE @Year int,@Month int,@Day intDECLARE @Hour int,@Minutes int,@Seconds intDECLARE @FractionsOfASecond intDECLARE @HourOffSet int,@MinuteOffSet intSET @Year =  -SET @Month =  GenevaSET @Day =  -SET @Hour =  theSET @Minutes =  $SET @Seconds =  theSET @FractionsOfASecond =  -SET @HourOffSet = 7SET @MinuteOffSet =  -SELECTDatetimeoffsetfromparts (@Year,@Month,@Day,@Hour,@Minutes,@Seconds,@FractionsOfASecond,@HourOffSet,@MinuteOffSet,3) asMytimezone

SQL Server->> time functions: EOMONTH, Datefromparts, Timefromparts, Datetimefromparts, Datetimeoffsetfromparts

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.