Sqlserver
 SQL >> Database >  >> RDS >> Sqlserver

Formattare una data come mm/dd/yyyy hh:mm:ss AM/PM in SQL Server

Su SQL Server 2012 è disponibile la funzione FORMAT, che consente di ottenere il formato richiesto in modo semplice.

DECLARE @date DATETIME = '2014-09-26 11:04:54'; SELECT FORMAT(@date, 'MM/dd/yyyy hh:mm:s tt');

Il risultato sarà:

09/26/2014 11:04:54 AM

Applicata a una colonna:

SELECT FORMAT(EntryDate, 'MM/dd/yyyy hh:mm:s tt') FROM DB1;

hh produce un orario a 12 ore, mentre tt aggiunge il designatore AM/PM.