前言
本文主要介紹的是使用 FORMAT函數將日期/時間和數字值格式化為識別區域設置的字符串。下面話不多說,來看詳細的介紹吧。
格式如下:
format(value,format,culture)
第一個參數是要格式化的值,第二個是格式,第三個是區域,比如是中國,還是美國,還是大不列顛等等。
FORMAT 依賴于 .NET Framework公共語言運行時 (CLR) 的存在。
declare @date datetime = '2014-01-01' select FORMAT( @date, 'd', 'en-US' ) as 'US English Result' ,FORMAT( @date, 'd', 'en-gb' ) as 'Great Britain English Result' ,FORMAT( @date, 'd', 'de-de' ) as 'German Result' ,FORMAT( @date, 'd', 'zh-cn' ) as 'Simplified Chinese (PRC) Result'; select FORMAT( @date, 'D', 'en-US' ) as 'US English Result' ,FORMAT( @date, 'D', 'en-gb' ) as 'Great Britain English Result' ,FORMAT( @date, 'D', 'de-de' ) as 'German Result' ,FORMAT( @date, 'D', 'zh-cn' ) as 'Chinese (Simplified PRC) Result'; /* USEnglish Result Great BritainEnglish Result German Result Simplified Chinese (PRC) Result ------------------------------------------------------------- ------------------------------------------------------------ 1/1/2014 01/01/2014 01.01.2014 2014/1/1 USEnglish Result Great BritainEnglish Result German Result Chinese (Simplified PRC) Result ------------------------------------------------------------- ------------------------------------------------------------ Wednesday,January 01, 2014 01 January 2014 Mittwoch, 1. Januar 2014 2014年1月1日 */
實例介紹
如果說我想要得到'2014年01月01日的結果,怎么得到呢?
select FORMAT( @date, 'yyyy年MM月dd日', 'zh-cn') as 當前日期 /* 當前日期 -------------------- 2014年01月01日 */
FORMAT除了日期以外,還可以處理一些數字格式和貨幣格式類型的轉換
if object_id('[tb]') is not null drop table [tb] create table [tb]([id] int,[NumericValue] numeric(3,2)) insert [tb] select 1,1.26 union all select 2,2.78 union all select 3,9.83 select *, FORMAT([NumericValue], 'G', 'en-us') as 'General Format', FORMAT([NumericValue], 'C', 'en-us') as 'Currency Format', FORMAT([NumericValue], 'G', 'de-de') as 'General Format', FORMAT([NumericValue], 'C', 'de-de') as 'Currency Format' from [tb] /* id NumericValue General Format Currency Format General Format Currency Format ------------------- ---------------- ----------------- ----------------------------------------- 1 1.26 1.26 $1.26 1,26 1,26
主站蜘蛛池模板:
大庆市|
宁津县|
泽库县|
齐齐哈尔市|
新巴尔虎右旗|
清徐县|
凤阳县|
临颍县|
台前县|
大洼县|
宣武区|
通山县|
稷山县|
武穴市|
南投县|
宣城市|
邵阳市|
高要市|
嵩明县|
彰化县|
杂多县|
塘沽区|
天柱县|
秦安县|
西贡区|
商洛市|
田阳县|
镇赉县|
微博|
达州市|
九江市|
漠河县|
通江县|
行唐县|
安吉县|
绵阳市|
昌图县|
望都县|
乐东|
榕江县|
古丈县|