Technical Article

Convert datetime to bigint

,

This function is used to generate the bigint value for given datetime.

Using this function value in database tables can improve the performance for our business rules by creating nonclustered index or unique nonclustered index .

Check this out....

select dbo.fn_generate_bigint(getdate())

Regards,

Vignesh Arulmani

create function fn_generate_bigint(@datetime datetime)
returns bigint
as
begin

declare @millisecond int
declare @calculateddatetime bigint
select  @millisecond = DATEPART(millisecond, @datetime)
select  @calculateddatetime =   DATEPART(second, @datetime) * 1 +
DATEPART(minute, @datetime) * 100 +
DATEPART(hour, @datetime) * 10000 +
DATEPART(day, @datetime) * 1000000 +
DATEPART(month, @datetime) * 100000000 +
DATEPART(year, @datetime) * 10000000000

return (@calculateddatetime * 1000 + @millisecond)

end
go

--examples
select dbo.fn_generate_bigint(getdate())

Rate

2.67 (6)

You rated this post out of 5. Change rating

Share

Share

Rate

2.67 (6)

You rated this post out of 5. Change rating