sqlserver------对日期格式的查询


 

对日期格式的模糊查询:
   需要借助convent 函数将一种数据类型的表达式转换为另一种数据类型的表达式,

直接进行查询的时候都可以查询出但是  1 不起左右,,使用2019_01的话查不出来

select * from dbo.tbmdKaoQingInfo where YearMonth like '%2019_1%'    

select * from dbo.tbmdKaoQingInfo where YearMonth like '%2019%'

如下图:

这也不行

select * from dbo.tbmdKaoQingInfo where YearMonth like '%2019-1%'

也不行

select * from dbo.tbmdKaoQingInfo where YearMonth like '%2019-01%'

建议使用  convert  进行转化一下,年月日就都有用了

CAST 和 CONVERT

将某种数据类型的表达式显式转换为另一种数据类型。CAST 和 CONVERT 提供相似的功能。

使用 CAST:CAST ( expression AS data_type )

使用 CONVERT:CONVERT (data_type[(length)], expression [, style])

 

select * from dbo.tbmdKaoQingInfo where CONVERT(varchar,YearMonth,120) like '%2019-01%'

日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型);或者字符串格式样式,借以将 float、real、money 或 smallmoney 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型)。

SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。

在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。

 

 

 

 

dfd


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM