Tag Archives: sql server date format

fecha a texto sql server / date to text sql server / 文字日期


Normalmente las bases de datos SQL Server están normalizadas para que cuando hacemos un select sobre un campo fecha nos retorne en el formato local que necesitamos, año/mes/día – mes/día/año – año-mes-día, etc. pero si necesitamos un formato específico y además en formato texto esta es una forma fácil de hacerlo:

FormatSQL QueryExample
1select convert(varchar, getdate(), 1)12/31/19
2select convert(varchar, getdate(), 2)19.12.31
3select convert(varchar,getdate(),3)31/12/19
4select convert(varchar,getdate(),4)31.12.19
5select convert(varchar,getdate(),5)31-12-19
6select convert(varchar,getdate(),6)31 Dec 19
7select convert(varchar,getdate(),7)Dec 31, 19
10select convert(varchar,getdate(),10)12-31-19
11select convert(varchar,getdate(),11)19/12/31
12select convert(varchar,getdate(),12)191231
23select convert(varchar,getdate(),23)2019-12-31
101select convert(varchar,getdate(),101)12/31/2019
102select convert(varchar,getdate(),102)2019.12.31
103select convert(varchar,getdate(),103)31/12/2019
104select convert(varchar,getdate(),104)31.12.2019
105select convert(varchar,getdate(),105)31-12-2019
106select convert(varchar,getdate(),106)31 Dec 2019
107select convert(varchar,getdate(),107)Dec 31, 2019
110select convert(varchar,getdate(),110)12-31-2019
111select convert(varchar,getdate(),111)2019/12/31
112select convert(varchar,getdate(),112)20191231
Date to text format

El que hace el truco es el convert y la clave el tercer parámetro, según este se devuelve uno u otro string.

The one that does the trick is the convert and the key is the third parameter, according to which one or the other string is returned.