Getting only Month and Year from SQL DATE(从 SQL DATE 仅获取月份和年份)
问题描述
我只需要从 SQL Server 的日期字段中访问 Month.Year.
I need to access only Month.Year from Date field in SQL Server.
推荐答案
除了已经给出的建议,我可以从您的问题中推断出另一种可能性:
- 你仍然希望结果是一个日期
- 但您想丢弃"日期、时间等
- 只留下年/月日期字段
As well as the suggestions given already, there is one other possiblity I can infer from your question:
- You still want the result to be a date
- But you want to 'discard' the Days, Hours, etc
- Leaving a year/month only date field
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, <dateField>), 0) AS [year_month_date_field]
FROM
<your_table>
这会从基准日期 (0) 获取整月数,然后将它们添加到该基准日期.因此向下舍入到日期所在的月份.
This gets the number of whole months from a base date (0) and then adds them to that base date. Thus rounding Down to the month in which the date is in.
注意:在 SQL Server 2008 中,您仍会将 TIME 附加为 00:00:00.000这与完全删除"任何日期和时间符号并不完全相同.也将 DAY 设置为第一个.例如2009-10-01 00:00:00.000
NOTE: In SQL Server 2008, You will still have the TIME attached as 00:00:00.000 This is not exactly the same as "removing" any notation of day and time altogether. Also the DAY set to the first. e.g. 2009-10-01 00:00:00.000
这篇关于从 SQL DATE 仅获取月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 SQL DATE 仅获取月份和年份
基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
