sql自定义函数


CREATE      function TL_getMode(@modeid nvarchar(100))
returns nvarchar(500)
as
begin
 declare @re nvarchar(500)
 set @re=''
 if(@modeid!='')
  begin
   select @re=@re+ModeName+'  ' from TL_Mode where charindex(','+cast(id as nvarchar(20))+',',','+@modeid+',')>0
   if(@re!='')
    begin
     if(RIGHT(@re,1)=',')
     select @re=left(@re, len(@re)-1) 
    end
   else
   set @re=''
  end
 else
  set @re=''
  return @re
end

 

  

将某种数据类型的表达式显式转换为另一种数据类型。 CAST CONVERT 提供相似的功能。
语法
使用 CAST
CAST ( expression AS data_type )
使用 CONVERT
CONVERT (data_type[(length)], expression [, style])
参数
expression
是任何有效的 Microsoft® SQL Server™ 表达式。
data_type
目标系统所提供的数据类型,包括 bigint sql_variant 。不能使用用户定义的数据类型
length
nchar nvarchar char varchar binary varbinary 数据类型的可选参数。
style
日期格式样式,借以将 datetime smalldatetime 数据转换为字符数据( nchar nvarchar char varchar nchar nvarchar 数据类型);或者字符串格式样式,借以将 float real money smallmoney 数据转换为字符数据( nchar nvarchar char varchar nchar nvarchar 数据类型)。
SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。
在表中,左侧的两列表示将 datetime smalldatetime 转换为字符数据的 style 值。给 style 值加 100 ,可获得包括世纪数位的四位年份 (yyyy)

不带世纪数位 (yy)
带世纪数位 (yyyy)

标准

输入 / 输出 **
-
0 100 (*)
默认值
mon dd yyyy hh:miAM (或 PM
1
101
美国
mm/dd/yyyy
2
102
ANSI
yy.mm.dd
3
103
英国 / 法国
dd/mm/yy
4
104
德国
dd.mm.yy
5
105
意大利
dd-mm-yy
6
106
-
dd mon yy
7
107
-
mon dd, yy
8
108
-
hh:mm:ss
-
9 109 (*)
默认值 + 毫秒
mon dd yyyy hh:mi:ss:mmmAM (或 PM
10
110
美国
mm-dd-yy
11
111
日本
yy/mm/dd
12
112
ISO
yymmdd
-
13 113 (*)
欧洲默认值 + 毫秒
dd mon yyyy hh:mm:ss:mmm(24h)
14
114
-
hh:mi:ss:mmm(24h)
-
20 120 (*)
ODBC 规范
yyyy-mm-dd hh:mm:ss[.fff]
-
21 121 (*)
ODBC 规范(带毫秒)
yyyy-mm-dd hh:mm:ss[.fff]
-
126(***)
ISO8601
yyyy-mm-dd Thh:mm:ss:mmm (不含空格)
-
130*
科威特
dd mon yyyy hh:mi:ss:mmmAM
-
131*
科威特
dd/mm/yy hh:mi:ss:mmmAM

 

*    默认值(style 0 1009 10913 11320 12021 121)始终返回世纪数位 (yyyy)

 

SQL Server 2005 Books Online (September 2007)

CAST and CONVERT (Transact-SQL)

Updated: 17 July 2006

Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.

Transact-SQL Syntax Conventions

<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl079c39139,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl07img,"; </script> Syntax

Syntax for CAST: CAST ( expression AS data_type [ (length ) ]) Syntax for CONVERT: CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl098bcac5c,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl09img,"; </script> Arguments

expression

Is any valid expression.

data_type

Is the target system-supplied data type. This includes xml, bigint, and sql_variant. Alias data types cannot be used. For more information about available data types, see Data Types (Transact-SQL).

length

Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types. For CONVERT, if length is not specified, the default to 30 characters.

style

Is the style of the date format used to convert datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types), or to convert character data of known date or time formats to datetime or smalldatetime data; or the string format used to convert float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types). When style is NULL, the result returned is also NULL.

Note:

The styles listed in this topic are supported by SQL Server in the described combinations with CONVERT target data types. All other styles and combinations are not supported. Do not use any unsupported styles. Using an unsupported style or an unsupported combination of style and target data type might return an error or unreliable results. These results are not guaranteed across releases of SQL Server.

SQL Server supports the date format in Arabic style by using the Kuwaiti algorithm.

In the following table, the two columns on the left represent the style values for converting datetime or smalldatetime data to character data. Add 100 to a style value to obtain a four-place year that includes the century (yyyy).

Without century (yy) (1)

With century (yyyy)

Standard

Input/Output (3)

-

0 or 100 (1, 2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值