zoukankan      html  css  js  c++  java
  • sql server2012中的format

    【1】基本介绍

    本文主要介绍的是使用 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日 
    */

    【2】实例演示

    【2.1】得到'2014年01月01日的结果

    select FORMAT( getdate(), 'yyyy年MM月dd日', 'zh-cn') as 当前日期 

      

    【2.2】货币转换

    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 € 
    2 2.78 2.78 $2.78 2,78 2,78 € 
    3 9.83 9.83 $9.83 9,83 9,83 € 
    */
  • 相关阅读:
    技术沙龙.:主题为《代码解析Castle(IOC)应用实例 -开源CMS 系统Cuyahoga》
    Active Record和Domain Object + Dao
    SNMP++.NET 项目
    微软发布Windows Vista Tips and Tricks网站
    2007 Office System Video
    使用搜索引擎搜索结果
    我购买了一台acer笔记本
    有价值的杂志《MSDN杂志》中文版
    Spring2.0中文参考手册(中文版) [转自CSDN论坛]
    开源项目Generics.Net介绍
  • 原文地址:https://www.cnblogs.com/gered/p/12881689.html
Copyright © 2011-2022 走看看