当前位置:网站首页>SQL Server data type conversion function cast () and convert () explanation
SQL Server data type conversion function cast () and convert () explanation
2022-07-30 03:59:00 【emgexgb_sef】
In SQL Server, data type conversion functions, commonly used functions are cast() and convert().
- Comparison of cast() and convert() functions:
(1)cast is generally easier to use, and the advantage of convert is that it can format dates and values;
(2) convert is generally used to convert between date and time types and decimals, while cast is generally used to convert decimal values and characters;
(3) convert is just a display conversion, and cast is a forced conversion.
One, cast() function
The cast() function is very simple to use, the syntax is:
cast(field name as conversion type)
Example:
select cast(123.45?as int)? ? ? ?--123, convert float type to int typeselect cast('123.447654' as int)????????-- Msg 245, Level 16, State 1, Line 1, Failed converting varchar value '2017.3' to data type int./*123.45 is of type float, and '123.447654' is of type char,Strings with decimal places cannot be directly converted to int type, but can be directly converted to decimal type.*/select cast('123.447654' as decimal)????????--123, decimal means that only the integer part is retained, and the fractional part is not retained.select cast('123.447654' as decimal(5,2))? ? ? ?/*123.45, convert the char type to a numeric type, 5 is the precision, which means the longest is 5 digits except the decimal point;2 is the number of decimal places, which means that there are two decimal places after the decimal point.And if you write select cast('123.447654' as decimal(5,3)),The error "Arithmetic overflow occurred while converting varchar to data type numeric" will be reported.Because during type conversion, the integer part will be satisfied first, and the integer part in '123.447654' already has 3 bits,The decimal part can only have 2 digits at most, and 3 decimal places cannot be reserved, all of which will report an error.At this time, it is necessary to improve the accuracy, that is, increase the value of 5.*/select cast('20210922' as date)? ? ? ?--2021-09-22, convert char type to date typeselect cast(getdate() as time)? ? ? ?--22:07:08.4200000, get the current time information without keeping the dateSecond, convert() function
Syntax:
convert(data_type(length),expression,style)
value
Description
data_type(length)
Specifies the target data type (with an optional length).
expression
Specifies the value to be converted.
style
Specifies the output format of the date/time.
The following table shows the style values of datetime or smalldatetime converted to character data:
Value
(century yy)
Value
(century yyyy)
input/output
Standard
-
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
Example:
SELECT CONVERT(INT, 25.65) --25select CONVERT(decimal, '123.45') -- 123select CONVERT(decimal(9,2), '123.45') -- 123.45SELECT CONVERT(VARCHAR(25),GETDATE()) --09 22 2021 10:31PMSELECT CONVERT(VARCHAR(24),GETDATE(),120) --2021-09-22 22:32:43, most commonly used, keep date and time informationSELECT CONVERT(VARCHAR(10),GETDATE(),102) --2021.09.22, only keep date informationSELECT CONVERT(VARCHAR(10),GETDATE(),111) --2021/09/22, only keep date informationSELECT CONVERT(VARCHAR(10),GETDATE(),108) --22:34:06, only keep time informationRefer to: SQL Server CONVERT() Function|Rookie Tutorial
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢

Microservice CAP Principles

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report

小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板

小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板

Nacos installation and deployment

Pytorch框架学习记录4——数据集的使用(torchvision.dataset)

Monitor page deployment

EasyCVR启动时报错“no such file or directory”,该如何解决?

逆向理论知识3【UI修改篇】

Nacos namespace
随机推荐
Advanced Microservices Cloud Alibaba
Pytorch框架学习记录3——Transform的使用
spicy(二)unit hooks
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
day10--install mysql on linux
第51篇-知乎请求头参数分析【2022-07-28】
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
Taobao/Tmall get the list of sold product orders API
Tcp编程
Nacos installation and deployment
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
2022.7.29-----leetcode.593
MySQ deadlock
Pytorch framework learning record 1 - Dataset class code combat
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report
ospf 综合实验(重发布,特殊区域)
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
Pytorch framework learning record 3 - the use of Transform
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d