当前位置:网站首页>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
边栏推荐
- Nacos service registration and discovery
- 2022.7.29-----leetcode.593
- 【驱动】udev设置GPIO加载后所有者、所属组和权限
- Boutique: Taobao/Tmall Get Order Details API for Purchased Products
- Operational configuration: How to run multiple EasyCVR programs as a service in one server?
- 小程序毕设作品之微信二手交易小程序毕业设计成品(4)开题报告
- Pytorch framework learning record 3 - the use of Transform
- 解决编译安装gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.“ 问题
- List获取差集产生的问题
- Smart answer function, CRMEB knowledge payment system must have!
猜你喜欢

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

Wechat second-hand transaction small program graduation design finished product (1) Development overview

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions

Pytorch framework learning record 2 - the use of TensorBoard

Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!

Pytorch框架学习记录7——卷积层

【C进阶】数组传参与函数指针

What is the difference between mission, vision and values?

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT

Has been empty, a straightforward, continue to copy the top off!
随机推荐
MySQ deadlock
WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
OA Project Pending Meeting & History Meeting & All Meetings
flutter 记录学习不一样的动画(二)
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
Nacos cluster partition
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
Send it to your friends and let TA treat you to fried chicken!
ospf 综合实验(重发布,特殊区域)
ospf map
Forum management system
Tcp programming
函数的底层机制
LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
vscode debugging and remote
SDL player in action
Pytorch framework learning record 3 - the use of Transform
【驱动】udev为USB转4串口的每个串口起别名
Anti-shake and throttling