This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE AdventureWorks2014 | |
SELECT CONVERT(char(7), OrderDate,21) AS 訂單月份, | |
CONVERT(char(10), OrderDate, 21) AS 訂單日期,* | |
FROM Sales.SalesOrderHeader | |
ORDER BY OrderDate DESC |

CONVERT(char(7), OrderDate,21)的21改成121、20、120的執行結果是一樣的
char(7)取年月,char(10)取年月日
CONVERT用法見MSDN網站說明
REF:
善用 SQL Server 中的 CONVERT 函數處理日期字串
下載Adventure Works 2014 Sample Databases:
https://msftdbprodsamples.codeplex.com/releases/view/125550
沒有留言:
張貼留言