开发者

Date format date-month-year

I have a table which has a datetime column. I want to show date in date-month-year forma.I 开发者_如何学Pythonam using SQL Server 2008.


You need to convert datetime column

select convert(varchar,datecolumn,103) from yourtable


Some datetime convertions:

SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy

SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
/*  YYYY/MM/DD
    2015/07/11    */
SELECT CONVERT(VARCHAR(10), GETDATE(), 112) AS [YYYYMMDD]
/*  YYYYMMDD
    20150711     */

-- SQL convert date string to datetime - time set to 00:00:00.000 or 12:00AM

PRINT CONVERT(datetime,'07-10-2012',110)        -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'2012/07/10',111)        -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'20120710',  112)        -- Jul 10 2012 

You can learn all DateTime convertion from here


FORMAT can be used for this;

SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') --20-09-2021

in your case;

SELECT FORMAT(datetime, 'dd-MM-yyyy') AS datetime --20-09-2021

Edit; Sorry to inform that I've just seen you mentioned SQL Server 2008 this code works for SQL Server 2012 for those who use 2012 and ends up in here!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜