赞
踩
对某些表格数据进行查询时,常常有按照时间进行列值过滤的需求。
SQL Server 内置函数CONVERT(data_type(length),data_to_be_converted,style)
常见的两种转换需求:
1. 日期 --> 字符串
2. 字符串 --> 日期
select getdate(); -- datetime
-- datetime --> string
declare @datetimeValue datetime = getdate();
select @datetimeValue,
convert(nvarchar(30), @datetimeValue, 120),
convert(nvarchar(30), @datetimeValue, 121),
convert(nvarchar(30), @datetimeValue, 126);
-- string --> datetime
declare @strValue nvarchar(30) = '2021-06-20 08:49:09.090';
select convert(datetime, @strValue, 121);
SQL Server 中的两个格式转换函数
CONVERT(data_type(length),data_to_be_converted,style)
CAST (expression AS data_type)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。