Assuming I have the following Date/Time in one tabla
of the Database:
Input :
05/03/2018 08:00:00.000
, Output :05/03/2018 18:00:00.000
I need to get the following output:
Entrada
: 08:00:00 a.m.
Salida
: 06:00:00 p.m.
I have the following:
select case when datepart(hour,entrada) > 12 then convert(varchar(28), cast(entrada as datetime), 8) + ' p.m.'
else convert(varchar(30), cast(entrada as datetime) , 8) + ' a.m.' end as hora_entrada,
case when datepart(hour,salida) > 12 then convert(varchar(28), cast(salida as datetime), 8) + ' p.m.'
else convert(varchar(30), cast(salida as datetime) , 8) + ' a.m.' end as hora_salida,
right(convert(varchar(20),entrada,113),9) as entrada,
right(convert(varchar(20),salida,113),9) as salida
from tablaX
The result obtained is:
As you can see the output is wrong 18:00:00 p.m.
.
How can I do it without using so many conversions?
Environment: SQL Server 2008 R2
The only thing you need is to specify the output format, which in this case is a
TIME
(NODATETIME
, so first you have to convert it to such) and with format 100 (you can see many different examples here ).In case anyone needs it, it ended up like this:
Solution 1:
Solution 2: