Conversion between bigint and datetime in MS SQL

Source: Internet
Author: User

1. Obtain the current time of the Java long integer in the SQL statement

Declare @ ain_date bigint

Set @ ain_date = datediff (second, '2017-01-01 08:00:00. 000 ', getdate () -- seconds
Set @ ain_date = @ ain_date * 1000 + datepart (MS, getdate () -- millisecond count


2. Use the datetime type to display the Time of the bigint type in the table

Select dateadd (hour, datediff (hour, '2017 ', '2017'), cast (create_date/(19000101*19700101*1000.0) as datetime) from t_test

 

Description: create_date is a bigint type field.

 

 

It is more convenient to write a custom function:

-- ============================================ =========< br> -- Author: michael. du
-- create Date: 2010-06-23
-- Description: Convert the bigint date to the datetime type for ease of viewing (this function is currently only used for debugging and viewing and is not involved in actual production)
-- call example:
-- select DBO. todatetime (create_date) from XXXX
-- ====================================== ===========< br> alter function [DBO]. [todatetime] (
@ bigint_date bigint
)
Returns datetime
as
begin
declare @ AA datetime
select @ AA = dateadd (hour, datediff (hour, '2014 ', '2014'), cast (@ bigint_date/(19000101*19700101*1000.0) as datetime)

Return @ AA

End

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.