简体   繁体   中英

Convert SQL Server Date Format

I'm using this query:

select convert(nvarchar(MAX), getdate(), 100);

It's returning

Aug 16 2018  3:45PM

I'm trying to get this date format instead:

16 AUG 15:45

Please help me achieve it. Thanks

试试这个:带格式功能

SELECT upper(FORMAT( getdate(), 'dd MMM HH:mm', 'en-US')) 

This gets you what you want, and without using the awfully slow FORMAT function:

SELECT D, CONVERT(varchar(6),DATEADD(HOUR, -1, D),13) + ' ' + CONVERT(varchar(5),CONVERT(time(0),DATEADD(HOUR, -1, D)),14)
FROM (VALUES(CONVERT(datetime2(0),'2018-08-16T15:45:00'))) V(d);

Edit: Seems the OP moved the goals posts again (initially they wanted the time changed from 01:38 AM to 13:38, and then 03:45 PM to 14:45), and as a result DATEADD isn't required. I haven't bothered removing this though, as it was correct at the time; and I don't trust the goal posts won't move again.

use upper and ' dd MMM HH:mm', 'en-US' format

SELECT Upper( FORMAT( getdate(), 'dd MMM HH:mm', 'en-US' ))

it reutrns 16 AUG 11:03

Try this

upper case 'Aug' to 'AUG'

SELECT UPPER( FORMAT( GETUTCDATE(), 'dd MMM HH:mm', 'en-US' ) )

Try this

Query:

select UPPER(FOrmat(GETDATE(),'dd MMM HH:mm'))

Result: 16 AUG 17:04

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM