Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting DateTime to String in SQL Server & Postgres

I've datetime value 2020-06-29 18:12:23 and I am trying to convert this to 20200629181223format in both SQL Server & Postgres

Any thoughts please? Thanks!

like image 596
Yash Avatar asked Sep 01 '25 16:09

Yash


2 Answers

You can also use to_char method like following on postgres

to_char(created_date, 'dd/mm/yyyy')

created_date is of type timezone with timezone here

You can read more use cases here

like image 167
Lalit Vavdara Avatar answered Sep 04 '25 06:09

Lalit Vavdara


This is easy with a convert and nested replace functions:

select replace(replace(replace(convert(varchar,getdate(),120),' ',''),'-',''),':','')
like image 45
TomC Avatar answered Sep 04 '25 05:09

TomC