Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Format and Convert functions in a select statement

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2º: CONVERT(VARCHAR(10), pb.FFIN, 103) AS [DD/MM/YYYY]

This give me a few questions:

  1. What are the main differences between using a FORMAT or a CONVERT in a select statement.

  2. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading