Solved

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

Posted on 2010-09-22
4
518 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now