从 SQL Server 2005 中的日期时间获取月份和年份 [英] Get month and year from a datetime in SQL Server 2005

查看:36
本文介绍了从 SQL Server 2005 中的日期时间获取月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 SQL Server 中日期时间的月+年,例如2008 年 1 月".我按月、年对查询进行分组.我已经搜索并找到了诸如 datepart、convert 等函数,但似乎没有一个对此有用.我在这里错过了什么吗?有这个功能吗?

I need the month+year from the datetime in SQL Server like 'Jan 2008'. I'm grouping the query by month, year. I've searched and found functions like datepart, convert, etc., but none of them seem useful for this. Am I missing something here? Is there a function for this?

推荐答案

如果您的意思是希望它们以字符串形式返回,请采用该格式;

If you mean you want them back as a string, in that format;

SELECT 
  CONVERT(CHAR(4), date_of_birth, 100) + CONVERT(CHAR(4), date_of_birth, 120) 
FROM customers

这里是其他格式选项

这篇关于从 SQL Server 2005 中的日期时间获取月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆