如何将短月份名称转换为月份的最后日期 [英] How to convert short month name to last date of month
问题描述
我有一个包含5列的SQL Server表
结构如下
-------- -----------------------------------
ID月份Emp_ID计数年
------------------------------------- ------
01 | Jan | 1 | 1.500 | 2015
02 |二月| 1 | 1.500 | 2015
03 | Mar | 1 | 1.500 | 2015
04 |四月| 1 | 1.500 | 2015
05 |五月| 1 | 1.500 | 2015
06 |君| 1 | 1.500 | 2015
07 |七月| 1 | 1.500 | 2015
08 |八月| 1 | 1.500 | 2015
09 |九月| 1 | 1.500 | 2015
10 |十月| 1 | 1.500 | 2015
11 | 11月| 1 | 1.500 | 2015
12 | 12月| 1 | 1.500 | 2015
我想将第二列转换成这样的
--------------- ----------------------------
ID月份Emp_ID计数年度
-------------------------------------------
01 | 2015年1月31日| 1 | 1.500 | 2015
02 | 2015年2月28日| 1 | 1.500 | 2015
03 | 2015年3月31日| 1 | 1.500 | 2015
04 | 2015年4月30日| 1 | 1.500 | 2015
05 | 2015年5月31日| 1 | 1.500 | 2015
06 | 2015年6月30日| 1 | 1.500 | 2015
07 | 2015年7月31日| 1 | 1.500 | 2015
08 | 2015年8月31日| 1 | 1.500 | 2015
09 | 2015年9月30日| 1 | 1.500 | 2015
10 | 2015年10月31日| 1 | 1.500 | 2015
11 | 2015年11月30日| 1 | 1.500 | 2015
12 | 2015年12月31日| 1 | 1.500 | 2015
无论如何都要实现这个目标吗?
I have a SQL Server table which contains 5 columns
The structure is as follows
-------------------------------------------
ID Month Emp_ID Count Year
-------------------------------------------
01 | Jan | 1 | 1.500 | 2015
02 | Feb | 1 | 1.500 | 2015
03 | Mar | 1 | 1.500 | 2015
04 | Apr | 1 | 1.500 | 2015
05 | May | 1 | 1.500 | 2015
06 | Jun | 1 | 1.500 | 2015
07 | Jul | 1 | 1.500 | 2015
08 | Aug | 1 | 1.500 | 2015
09 | Sep | 1 | 1.500 | 2015
10 | Oct | 1 | 1.500 | 2015
11 | Nov | 1 | 1.500 | 2015
12 | Dec | 1 | 1.500 | 2015
I want to convert the second column to like this
-------------------------------------------
ID Month Emp_ID Count Year
-------------------------------------------
01 | 31-Jan-2015 | 1 | 1.500 | 2015
02 | 28-Feb-2015 | 1 | 1.500 | 2015
03 | 31-Mar-2015 | 1 | 1.500 | 2015
04 | 30-Apr-2015 | 1 | 1.500 | 2015
05 | 31-May-2015 | 1 | 1.500 | 2015
06 | 30-Jun-2015 | 1 | 1.500 | 2015
07 | 31-Jul-2015 | 1 | 1.500 | 2015
08 | 31-Aug-2015 | 1 | 1.500 | 2015
09 | 30-Sep-2015 | 1 | 1.500 | 2015
10 | 31-Oct-2015 | 1 | 1.500 | 2015
11 | 30-Nov-2015 | 1 | 1.500 | 2015
12 | 31-Dec-2015 | 1 | 1.500 | 2015
Is there anyway to achieve this?
推荐答案
如果你可以计算所有的一个月内的天数,然后用您想要的格式替换月份名称?
If you can do a count for all the days in a month and then replace the month name with the format you wanted?
您不应将月份存储为字符串(varchar
或任何类似的东西);您不应该使用数字和字符串属性在表中存储与时间相关的数据;它是非常不受支持的。使用适当的数据类型,例如DATE
。然后可以在您的客户端代码中确定日期/时间数据的特定形式(字符串表示),甚至不是SQL代码。
-SA
You should not store month as string (varchar
or anything like that); you should not store time-related data in the table with numeric and string attributes; it is quite unsupportable. Use appropriate data types, such asDATE
. Then the particular form (string representation) of date/time data can be decided in your client code, not even SQL code.
—SA
如果您使用的是SQL 2012或更高版本,则可以使用EOMONTH
功能 [ ^ ]获取一个月的最后一天。否则,从该月的第一天开始,添加一个月然后减去一天将得到相同的结果。
If you're using SQL 2012 or higher, you can use theEOMONTH
function[^] to get the last day in a month. Otherwise, starting from the first day in the month, adding one month and then subtracting one day will give you the same result.
SELECT
-- SQL 2012:
EOMONTH(Convert(date, Convert(char(4), Year) + '/' + Month + '/01')) As LastDayOfMonth
-- Earlier versions:
-- DateAdd(day, -1, DateAdd(month, 1, Convert(date, Convert(char(4), Year) + '/' + Month + '/01'))) As LastDayOfMonth
FROM
YourTable
正如谢尔盖所说,不要t将日期值存储在字符串列中;使用 日期
类型 [ ^ ]。
这篇关于如何将短月份名称转换为月份的最后日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!