当前位置:网站首页>SQL Server数据类型转换函数cast()和convert()详解
SQL Server数据类型转换函数cast()和convert()详解
2022-07-30 03:11:00 【emgexgb_sef】
SQL Server中,数据类型转换函数,常用的函数有cast()和convert()。
- cast()和convert()函数比较:
(1)cast一般更容易使用,convert的优点是可以格式化日期和数值;
(2)convert一般用于日期和时间类型以及小数之间转换,而cast一般用于小数转数值和字符型;
(3)convert只是显示转换,而cast是强制转换。
一、cast()函数
cast()函数使用起来非常简单,语法为:
cast(字段名 as 转换的类型 )
如:
select cast(123.45?as int)? ? ? ?
--123,将float类型转换为int类型
select cast('123.447654' as int)????????
--消息 245,级别 16,状态 1,第 1 行,在将 varchar 值 '2017.3' 转换成数据类型 int 时失败。
/*123.45为float类型,而'123.447654'为char类型,
带有小数位数的字符串无法直接转化为int类型,但可以直接转化为decimal类型。
*/
select cast('123.447654' as decimal)????????
--123,decimal代表只保留整数部分,不保留小数部分。
select cast('123.447654' as decimal(5,2))? ? ? ?
/*123.45,将char类型转换为数值类型,5为精度,代表除小数点外,最长为5位;
2为小数位数,代表小数点后面保留两位小数。
而如果写select cast('123.447654' as decimal(5,3)),
就会报“将 varchar 转换为数据类型 numeric 时出现算术溢出”的错误。
因为在类型转换时,会优先满足整数部分,而'123.447654'中整数部分已经有3位,
小数部分最多就只能有2位,无法保留3位小数,所有会报错。
这时候就需要提高精度,即将数值5增大。*/
select cast('20210922' as date)? ? ? ?
--2021-09-22,将char类型转换为日期类型
select cast(getdate() as time)? ? ? ?
--22:07:08.4200000,取当前的时间信息,不保留日期
二、convert()函数
语法:
convert(data_type(length),expression,style)
值
描述
data_type(length)
规定目标数据类型(带有可选的长度)。
expression
规定需要转换的值。
style
规定日期/时间的输出格式。
下面的表格展示了 datetime 或 smalldatetime 转换为字符数据的 style 值:
值
(世纪 yy)
值
(世纪 yyyy)
输入/输出
标准
-
0 or 100
mon dd yyyy hh:miAM (or PM)
Default
1
101
mm/dd/yy
USA
2
102
yy.mm.dd
ANSI
3
103
dd/mm/yy
British/French
4
104
dd.mm.yy
German
5
105
dd-mm-yy
Italian
6
106
dd mon yy
7
107
Mon dd, yy
8
108
hh:mm:ss
-
9 or 109
mon dd yyyy hh:mi:ss:mmmAM (or PM)
Default+millisec
10
110
mm-dd-yy
USA
11
111
yy/mm/dd
Japan
12
112
yymmdd
ISO
-
13 or 113
dd mon yyyy hh:mi:ss:mmm (24h)
14
114
hh:mi:ss:mmm (24h)
-
20 or 120
yyyy-mm-dd hh:mi:ss (24h)
-
21 or 121
yyyy-mm-dd hh:mi:ss.mmm (24h)
-
126
yyyy-mm-ddThh:mi:ss.mmm (no spaces)
ISO8601
-
130
dd mon yyyy hh:mi:ss:mmmAM
Hijiri
-
131
dd/mm/yy hh:mi:ss:mmmAM
Hijiri
如:
SELECT CONVERT(INT, 25.65) --25
select CONVERT(decimal, '123.45') -- 123
select CONVERT(decimal(9,2), '123.45') -- 123.45
SELECT CONVERT(VARCHAR(25),GETDATE()) --09 22 2021 10:31PM
SELECT CONVERT(VARCHAR(24),GETDATE(),120) --2021-09-22 22:32:43,最常用,保留日期时间信息
SELECT CONVERT(VARCHAR(10),GETDATE(),102) --2021.09.22,只保留日期信息
SELECT CONVERT(VARCHAR(10),GETDATE(),111) --2021/09/22,只保留日期信息
SELECT CONVERT(VARCHAR(10),GETDATE(),108) --22:34:06,只保留时间信息
参考于:SQL Server CONVERT() 函数 | 菜鸟教程
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 一本通1922——乒乓球
- 开放地址法哈希实现——二次探测法
- 3种实现文本复制功能的方法
- JUC (six): synchronized
- Zero code tools recommended - HiFlow
- Are you still using the command line to read logs?Quickly use Kibana, visual log analysis YYDS
- The speed of life and death, every second counts
- Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.
- Solve The problem of Google browser cross-domain has had been blocked by CORS policy: The request The client is not a secure context and The resou
- JIT VS AOT
猜你喜欢

Detailed explanation of carousel picture 2 - carousel pictures through left positioning

解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou

复星医药募资44.84亿:高毅资产认购20亿 成第三大股东

Ansible简介(详细)特性+优点+设计理念+应用领域+系统架构+工作原理+任务执行流程

阿里云EasyNLP中文文图生成模型带你秒变艺术家

HCIP OSPF

Are you still using the command line to read logs?Quickly use Kibana, visual log analysis YYDS

ESP8266 +0.96“ I2C OLED 表盘时钟

NLP自然语言处理(一)

NLP Natural Language Processing (2)
随机推荐
Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.
最重要的传输层
力扣(LeetCode)210. 课程表 II(2022.07.29)
Fudan-Washington University EMBA Kechuang's Ao E丨The Magical Materials and We Are Shaped
(RCE)远程代码/命令执行漏洞漏洞练习
Dataset之MNIST:MNIST(手写数字图片识别+ubyte.gz文件)数据集简介、下载、使用方法(包括数据增强)之详细攻略
JUC(七):变量的线程安全分析
复星医药募资44.84亿:高毅资产认购20亿 成第三大股东
Leetcode.24 两两交换链表中的节点(递归)
A. Strange Birthday Party- Codeforces Round #694 (Div. 1)
CF1473C No More Inversions
C# 一周入门之《C#-类和对象》Day Six
软件测试面试题及答案解析,2022最强版
零代码工具推荐---HiFlow
JUC (8) : synchronized little exercise
HCIP实验(05)OSPF综合实验
Successfully resolved AttributeError: 'PngImageFile' object has no attribute 'imshow'
WPF 学习笔记《WPF布局基础》
A plastic bottle of ocean "fantasy drifting"