如何从Sql获取月份名称 [英] How to get Month Name from Sql

查看:83
本文介绍了如何从Sql获取月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我的表中有一个月的Id列,插入1,2,3等值。



并且想要选择月份名称而不是整数值。



我正在尝试以下语法,



datename(month,MonthId)作为月份但是在所有coloumn中它只显示1月。



请告诉我如何获得结果。

解决方案

dataname函数需要一个日期作为第二个参数,试试这个技巧:

  SELECT  DATENAME(MONTH,DATEADD(月,月 -   1 ,CAST('  2014-01-01'  AS   DATETIME )))
FROM table1


那是因为DATENAME不接受月份号码 - 它接受es一个DATE值。因此,当你提供1到12之间的数字时,它假设是一年中的天数...

你可以通过确保月份是基于零的值来摆弄它并将它乘以30,但它是一个kludge,我不喜欢它...


你也可以从后面的代码中做到这一点



如下

使用System.Globalization



 DateTimeFormatInfo m =  new  DateTimeFormatInfo(); 
string 月= m.GetMonthName( int Month_No)


Hello,

I have an month Id column in my table and inserting values like 1,2,3 etc.

and want to select an month name instead of integer values.

I am trying the following syntax,

datename(month,MonthId)as Month but in all the coloumn it shows january only.

please tell me how to get the result.

解决方案

The dataname function expects a date as second parameter, try this trick:

SELECT DATENAME(MONTH, DATEADD(MONTH, monthid - 1, CAST('2014-01-01' AS DATETIME)))
FROM table1


That's because DATENAME doesn't accept a month number - it takes a DATE value. So when you provide it with a number between 1 and 12, it assumes that is the number of days into the year...
You could "fiddle" it by ensuring the month is a zero based value and multiplying it by 30, but it's a kludge and I don't like it much...


you can also do it from code behind

as below
using System.Globalization

DateTimeFormatInfo m = new DateTimeFormatInfo();
                string Month=m.GetMonthName(int Month_No)


这篇关于如何从Sql获取月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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