在 SQL Server 中将月份名称转换为月份编号 [英] Convert month name to month number in SQL Server

查看:33
本文介绍了在 SQL Server 中将月份名称转换为月份编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 T-SQL 中,将月份名称转换为数字的最佳方法是什么?

In T-SQL what is the best way to convert a month name into a number?

例如:

'January' -> 1
'February' -> 2
'March' -> 3

是否有任何内置函数可以做到这一点?

Are there any built in functions that can do this?

推荐答案

这个怎么样?

select DATEPART(MM,'january 01 2011') -- returns 1
select DATEPART(MM,'march 01 2011')  -- returns 3
select DATEPART(MM,'august 01 2011') -- returns 8

这篇关于在 SQL Server 中将月份名称转换为月份编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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