Date format in Millisecond.

GETDATE() in MS SQL return the standard format but I am interested in the number of millisecond from 1970 until now.   In MySQL there is the function UNIX_TIMESTAMP() that does this.  Is there a familiar functiontion to do this? i know it is simple but I cant seem to find it.
fylix0000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hi fylix0000,

The following works:

DECLARE @mili bigint
SET @mili=DATEDIFF(s, '1 Jan 1970', GETDATE())
SET @mili = @mili * 1000
SELECT @mili

It appears that trying to do it all in one expression will bomb because DATEDIFF returns type int, and that data type is
not big enough to hold all the miliseconds that have transpired since then...

Regards,

Patrick
0
fylix0000Author Commented:
Hmm, I was trying to use this function within the SQL stament.  SO i was looking for either a single function or some sort of DateFormat ( msec, GETDate())
0
Patrick MatthewsCommented:
fylix0000,

What is the SQL going to look like?

Regards,

Patrick
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

fylix0000Author Commented:
CREATE TEMPORARY TABLE acctmp
SELECT users.user_name, 0, UNIX_TIMESTAMP() * 1000
 FROM users LEFT JOIN accountStatus
ON users.user_name=accountStatus.user_name
WHERE status IS NULL;


INSERT INTO accountStatus SELECT * from acctmp;



Basically that is the original statement in MySQL, I am moving over to MS SQL 2000 and I am trying to convert the statement so it can work with MS SQL.  For some reason MS SQL does not like couple of things,

-UNIX_TIMESTAMP()
-TEMPORARY
-LEFT JOIN

I'm quite new to MS SQL so I am quite puzzle, is there a reference to MS SQL query language specifically?
0
Scott PletcherSenior DBACommented:
I think you will have to do this:


DECLARE @ms BIGINT
SET @ms = (CAST(DATEDIFF(SECOND, '19700101', GETDATE()) AS BIGINT) * 1000) + DATEDIFF(MS, CONVERT(CHAR(8), GETDATE(), 112), GETDATE())
SELECT @ms
0
Scott PletcherSenior DBACommented:
D'OH, sorry, forgot to put it into a function:


CREATE FUNCTION GetUnixTimestamp (@date DATETIME)
RETURNS BIGINT
AS
BEGIN
RETURN (CAST(DATEDIFF(SECOND, '19700101', @date) AS BIGINT) * 1000) + DATEDIFF(MS, CONVERT(CHAR(8), @date, 112), @date)
END --FUNCTION

0
Scott PletcherSenior DBACommented:
CORRECTION:

CREATE FUNCTION GetUnixTimestamp (@date DATETIME)
RETURNS BIGINT
AS
BEGIN
RETURN (CAST(DATEDIFF(SECOND, '19700101', @date) AS BIGINT) * 1000) + DATEPART(MS, @date)
END --FUNCTION
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.