I have a select and I need to format a few of it´s dates (in BBDD they are in YYYY-MM-DD and i need them in DD/MM/YYYY).
Looking around i found two different methods (both work OK)
1º: FORMAT(pb.FINICIO, 'dd/MM/yyyy') as finicio
2º: CONVERT(VARCHAR(10), pb.FFIN, 103) AS [DD/MM/YYYY]
This give me a few questions:
-
What are the main differences between using a FORMAT or a CONVERT in a select statement.
-
While it may be opinion based, which option is better? again, i am doing a select.
pd: the whole QUERY:
SELECT pb.IDFACT, pa.IDGRUPO, pa.CNOM, pa.CDESC, pa.CESTAD, pa.CABS, pa.FINICIO as finiciog,
pa.FFIN as ffing , pa.NMOV45, pc.TIP032, pc.NIF032,
pc.NOC032, pc.FCO032, pc.XVIA032, pc.XNOVIA32, pc.NVIA1032, pc.CPO032, pc.EMA032, FORMAT(pb.FINICIO, 'dd/MM/yyyy') as finicio,
CONVERT(VARCHAR(10), pb.FFIN, 103) AS [DD/MM/YYYY],
pc.TEL032, pc.OBS032, pc.SIT032, CONCAT(pc.ap1032 ,' ' ,pc.ap2032 , ', ' , pc.no032) as nombreCompleto, pc.noc032 as nombreCorporativo
FROM spt145 as pa
LEFT JOIN SPT144 as pb ON pa.IDGRUPO = pb.IDGRUPO and pb.SVIGE = 'S'
LEFT JOIN GACTB032 as pc ON pb.idFact = pc.FACT032 WHERE
pa.SVIGE = 'S'
>Solution :
Aaron Bertrand has article about diff but generally, the FORMAT is more powerful in terms of the control of the output but more expensive as operator.
So, I am using the CONVERT if possible and FORMAT only for output which is not available with the CONVERT.