How do I convert Last Login in DB (MSSQL) to date/time?

Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 16, 2012

I am trying to create a SQL query or modify the data in Excel after export from SQL which will give me the correct Date/Time for Last Login of users in the system. However, the Sample SQL queries reference a to_timestamp function which is an Oracle only function, and i haven't been able to figure out what date is the reference date for the Milliseconds or how to convert the milliseconds correctly to be added to that date.

I even reached back to Algrebra class knowledge, and created 2 equations with 2 variables in order to solve for them and calculate based on that, but values outside of my reference times are still inaccurate. Any help will be appreciated. Thanks.

DB: MSSQL 2008

5 answers

1 accepted

2 votes
Answer accepted
PauloP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2012
Hi Adam,

For MYSQL server, you may use the FROM_UNIXTIME function. Here is an example:

SELECT
theTimeStamp, FROM_UNIXTIME(theTimeStamp)AS readableDate FROM theTable WHERE theTable.theField = theValue;

About SQL server, I'm not quite sure, but you may try do that using a query lin the following:

SELECT
CONVERT(TIME, DATEADD(ms, SUM(duration/10000%1000), DATEADD(ss, SUM(duration/10000000),0)))FROM tblMediaFileProperties

In case of any problem, try to use a clone database in MYSQL server to double check it.

Cheers,

Paulo Renato
Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2012

From my original question, i specified at the end that we are using MSSQL 2008, therefore, functions for MYSQL do not work. I also don't think it would be very useful to temporarily copy my database to a MYSQL database just to run the query once because I am planning on running this on a regular basis.

Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2013

I apparently didn't read your whole post with the CONVERT part. I did have to modify the statement a little bit, but here is what i came up with which works.

CONVERT(DateTIME, DATEADD(ss, CAST(lastLoginMillis as Numeric(30))/1000, DATEADD(hh, -6,'1970-01-01'))) as LastLogin

Then i was able to get the correct date/time. I think the main thing that messed me up is that in the SQL examples that Atlassian provides, they reference the incorrect attribute to get the last login. They reference the PreviousLogin attribute which is why my dates were so wonky when comparing them to the values from within the administrator interface.

Like # people like this
Kevin Mowrey February 18, 2020

Thank you, this worked for me. :)

1 vote
PauloP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2012

Hi Adam

I believe that you may try to create a new SQL query using the following link as an example:

https://confluence.atlassian.com/display/JIRACOM/Example+SQL+queries+for+JIRA#ExampleSQLqueriesforJIRA-Listusername,fullname,emailaddress,logincount,andlasttimeloggedin

Cheers,

Paulo Renato

Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2012

That is what i meant with my original question when i mentioned "the Sample SQL queries reference a to_timestamp function which is an Oracle only function". That is obviously where i got the basics of my query and how to get the value for Milliseconds out of the database.

However, my question is regarding how to convert that Last Login (in Milliseconds) into a readable date/time? As i tried to explain in my original question is that i have tried the basics on how to convert this value to a valid date, but have not found anything that converts the values correctly for all users.

0 votes
Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2013

The column type in the database is nvarchar(255) and an example of a value is:

1366052401471

Which should equate to 2013-04-15 13:58:xx:xxx in the CDT timezone.

0 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2013

Can you tell the type of the column in the MS SQL database? And give a sample of the value from database?

0 votes
Adam Barylak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 2, 2013

Does anyone else have any suggestions? Please help. Thanks.

Suggest an answer

Log in or Sign up to answer