转换并转换 [英] casting and convert

查看:76
本文介绍了转换并转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#.net中强制转换和转换之间有什么区别
以及在SQL Server中进行转换和转换的方式
以及在SQL Server中强制转换和转换之间有什么区别
感谢您的帮助

what difference between casting and convert in c#.net
and what a casting and convert in sql server
and what difference between casting and convert in sql server
thanks for any help

推荐答案

铸造是指显式更改数据类型,而转换则是隐式更改数据类型.
C#和MSSQL之间的主要区别在于数据类型的名称以及两者的完成方式.
在C#
Casting refers to changing the data types EXPLICITLY while conversion is changing data types IMPLICITLY.
The main difference between C# and MSSQL are the names of the datatypes and the manner in which both are done.
In C#
public void myMethod ()
{
   int i = 9;
   float x = i; /*This is conversion as the value of i is implicitly converted to float*/
   int i = (int) (x+3.5); /*This is casting as float is explicitly converted into int via () operation*/
}



在MSSQL中



In MSSQL

INSERT INTO MyTable (Col1) VALUES (''200'')


如果Col1为int,float或任何其他数字格式,则字符串"200"将隐式转换


If Col1 is int, float or any other numeric format, the string ''200'' is implicitly converted

INSERT INTO MyTable (Col1) VALUES (CAST(''200'' as int))


这是MSSQL中显式强制转换的形式


This is the form of the explicit casting in MSSQL

INSERT INTO MyTable (Col1) VALUES (CONVERT(''200'', int))


我不确定最后一句的确切语法(请检查SQL Management Studio).CONVERT函数的行为类似于上一条语句中的CAST函数.主要区别在于CONVERT函数已重载以允许自定义格式,尤其是在转换为日期时间格式时(我忘记了确切的语法,并且在这台计算机上还没有Visual Studio ...对不起)

如果您喜欢此帖子,请接受它作为答案


I''m not sure about the exact syntax to this last one (Pls. check SQL Management Studio) The CONVERT Function acts like the CAST function in the previous statement. The main difference is that the CONVERT function is overloaded to allow custom formatting especially in converting to datetime formats (I forgot the exact syntax and don''t have Visual Studio on this computer yet... sorry)

If you like this post, please accept it as an answer


两者均用于Converion,Convert函数需要3个参数

Convert(varchar(20),mydate,103)

同时,Cast(Mydate as Varchar(20))

而在Cast引发错误时,Convert还可以处理Null情况

因此,请使用Convert ...
Both is used for Converion, Convert function takes 3 Parameters

Convert(varchar(20),mydate,103)

While, Cast(Mydate as Varchar(20))

And one more thing Convert handles Null case while Cast raises error

So Use Convert...


Convert(DataType,UrExpression,Style)

日期时间类型主要使用样式,并且dd/mm/yyyy代表103

因此,如果您使用
在SQL查询分析器中选择Convert(Varchar,Getdate(),103)作为MyDate,它将返回:

22/09/2010
Convert(DataType,UrExpression,Style)

Mainly style is used for Datetime Type, and 103 stands for dd/mm/yyyy

So if U r using
Select Convert(Varchar, Getdate(),103) as MyDate in SQL Query Analyser it will return:

22/09/2010


这篇关于转换并转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆