Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: How to get current date time in YYYYMMDDHHMISSMSS

I need the current date time in the format YYYYMMDDHHMISSMIS

Example:

20110723233747607

By using the CURRENT_TIMESTAMP or getdate() functions, we can retrieve the current datetime into 2011-07-23 23:37:47.607 format. If I use REPLACE and CONVERT functions to remove the "-" and ":" characters, then I get the value into

Jul 23 2011 11:37PM

...format. But I need the current date time as 20110723233747607 to use it for my another purpose.

My SQL query is:

SELECT REPLACE(CONVERT(VARCHAR(20), CURRENT_TIMESTAMP),'.','')

output: Jul 23 2011 11:37PM

So how can I get the current date time in my required format? Pls help.

like image 964
riad Avatar asked Jul 23 '11 17:07

riad


People also ask

How do I get the current datetime in SQL query?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format. Tip: Also look at the CURRENT_TIMESTAMP function.

How do I get the current timestamp in SQL Server?

The CURRENT_TIMESTAMP function returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the GETDATE() function.

How do I get the current date and time in SQL Developer?

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE . The time zone offset reflects the current local time of the SQL session.


2 Answers

select replace(
       replace(
       replace(
       replace(convert(varchar(23), getdate(), 121),
       '-',''),
       '.',''),
       ' ',''),
       ':','')
like image 124
Mikael Eriksson Avatar answered Oct 11 '22 08:10

Mikael Eriksson


I do not know why you need to use so many REPLACE() functions. Usage of functions really reduce the execution time. I used two CONVERT and one REPLACE function below.

SELECT CONVERT(VARCHAR(8), GETDATE(), 112) + REPLACE(CONVERT(VARCHAR(12), GETDATE(), 114),':','')
like image 1
user007 Avatar answered Oct 11 '22 09:10

user007