Convert time to number in T-SQL

I'm sure this is easy - how do you convert the current date and time into a number in Transact SQL, taking into account split seconds? Quite a few digits if possible as i want to use it as an  ID on an Audit table (not a unique ID though for obvious reasons!)- thanks for looking
fuerteventuraAsked:
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.

TimCotteeHead of Software ServicesCommented:
You can cast it:

select cast(getdate() as decimal(18,13))

For example. The larger the precision/scale, the finer the resolution of the result.
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
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT REPLACE(REPLACE(REPLACE(CONVERT (VARCHAR,GETDATE(),120),':',''),'-',''),' ','')
0
Ashish PatelCommented:
just an addition in above query

SELECT Cast(REPLACE(REPLACE(REPLACE(CONVERT (VARCHAR,GETDATE(),120),':',''),'-',''),' ','') as Numeric(18,0))
0
The Ultimate Tool Kit for Technolgy Solution Provi

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 for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

fuerteventuraAuthor Commented:
Many thanks for all your help, I know have further clarification on what is required - what i need is the following:

on 4th February 2008 at 11:30 and 20 seconds and 30 milliseconds I need an id such as the following:
0402200811302030

Can anyone help me get that/ I presume a datepart function may be usefull? I will be awarding points to all of the above as all answer the original question, many thanks again
0
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT REPLACE(CONVERT(VARCHAR, GETDATE() , 103),'/','')+
       REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')


0
Scott PletcherSenior DBACommented:
Instead of:

mmddyyyyhhmmssss

you should use:

yyyymmddhhmmssss

So:

SELECT REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16), GETDATE(), 121), '-', ''), ' ', ''), ':', ''), '.', '')

This allows direct sorting and means that new numbers will always be larger than earlier ones.

You can always re-format for display.  Don't allow the desired display format to make you use the wrong internal storage format :-) .
0
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.