Solved

How do Return a unique number from a datetime field in sql 2008

Posted on 2010-09-22
4
524 Views
Last Modified: 2012-05-10
Hi Gurus

I am pulling my hair out.  How do i return an integer that is unique based on date AND time from a date time field.  The value currently in the datetime field which is called datetimsubmitted is '2010/09/03 10:18:05 AM'

It is easy to return an integer from the date only but each record needs to be uniquely identified by an integer based on date and time to the second
many thanks
Alan
0
Comment
Question by:caandal
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 33733619
If you want to return a BigInt, based of DateTime value, that will be always unique

use

CAST(Convert(VARCHAR(50), GETDATE(), 112) + REPLACE(Convert(VARCHAR(50), GETDATE(), 114),':','') AS BIGINT)

To Test it write

SELECT CAST(Convert(VARCHAR(50), GETDATE(), 112) + REPLACE(Convert(VARCHAR(50), GETDATE(), 114),':','') AS BIGINT)

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33733634
How about seconds from date 0 (1900-01-01) ?

convert(bigint, convert(float, datetimesubmitted)*24*60*60)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33733655
Or the Unix timestamp, which is number of seconds from 1970-01-01 12:00am

convert(int, (convert(float, datetimesubmitted)-convert(float, convert(datetime,'19700101')))*24*60*60)
0
 

Author Closing Comment

by:caandal
ID: 33733678
Hi VjSoft
Thanks - I was one bracket away from the right solution and then chucked it out
Alan
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question