Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-22
4
Medium Priority
?
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 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