简体   繁体   English

如何获取月份(字符)和年份(日期)的名称以显示在我的数据项和Cognos的列中

[英]How do I get the name of the month (char) and the Year (date) to appear in my data item and column in Cognos

My Accounting Period Date is using a Numeric data type 我的会计期间日期正在使用数值数据类型

1990-01-01
2008-08-01
2008-07-01

So I applied CAST function in the data item. 所以我在数据项中应用了CAST功能。 在此处输入图片说明

And the results appeared like this. 结果看起来像这样。

Jan 1, 1990
Aug 1, 2008
Jul 1, 2008

Is there a way to make it appear like this? 有没有办法让它看起来像这样?

Jan 1990
Aug 2008
Jul 2008

I tried using a new data item using Replace Function like this but it does not work. 我尝试使用像这样的“替换功能”使用新的数据项,但是它不起作用。

This is the error I get. 这是我得到的错误。 在此处输入图片说明

Could someone help me please. 有人可以帮我吗。

Part of the problem you will run into is Cognos's auto formatting of date types. 您将遇到的部分问题是Cognos的日期类型自动格式化。 That's what you are seeing with the Jan 1, 1990 format. 这就是1990年1月1日格式的显示。 The data still exists as a date but Cognos chooses to reformat it when running. 数据仍然作为日期存在,但是Cognos选择在运行时重新格式化。 Thus, you cannot use a substring function to extract out the components you want as it doesn't exist as a string. 因此,您不能使用子字符串函数来提取所需的组件,因为它不以字符串形式存在。 Straight casting to a string reverts it to the 'YYYY-MM-DD' format in string form, obviously not what you want. 直接转换为字符串会将其恢复为字符串形式的'YYYY-MM-DD'格式,显然不是您想要的格式。

There are three options. 有三种选择。

If you just want the date to display in the desired format on the report, then the simplest solution is to use the 'Data Format' property on the column itself in the report definition (list, crosstab, singleton etc). 如果只希望日期以所需的格式显示在报表上,则最简单的解决方案是在报表定义(列表,交叉表,单例等)中使用列本身的“数据格式”属性。 Within the Data Format property sheet there is a property called 'Pattern'. 在“数据格式”属性表中,有一个名为“模式”的属性。 You can use this property to define how you want the column displayed. 您可以使用此属性来定义列的显示方式。 Enter 'MMM YYYY' in this property to create the output you desire. 在此属性中输入“ MMM YYYY”以创建所需的输出。

在此处输入图片说明

If you need the actual data item to change to match the format you require then you will have to build a string: 如果您需要更改实际的数据项以匹配所需的格式,则必须构建一个字符串:

CASE extract(month,[Date]) 
WHEN 1 THEN 'Jan' 
WHEN 2 THEN 'Feb' 
WHEN 3 THEN 'Mar' 
WHEN 4 THEN 'Apr' 
WHEN 5 THEN 'May' 
WHEN 6 THEN 'Jun' 
WHEN 7 THEN 'Jul' 
WHEN 8 THEN 'Aug' 
WHEN 9 THEN 'Sep' 
WHEN 10 THEN 'Oct' 
WHEN 11 THEN 'Nov' 
WHEN 12 THEN 'Dec' 
END || ' ' || cast(extract(year,[Date]),char(4))

Your last option is to use a data source vendor-specific cast function. 最后一个选择是使用数据源供应商特定的转换功能。 For example, DB2's to_Char() function allows you to precisely define how the date is converted to a char. 例如,DB2的to_Char()函数使您可以精确地定义如何将日期转换为char。 Other database servers have their own functions that do similar things. 其他数据库服务器具有自己的功能,可以执行类似的操作。 You'll have to figure out if there is an equivalent in your data source vendor's solution. 您必须弄清楚数据源供应商的解决方案中是否存在等效的解决方案。

The down side to this approach is that if you change data sources to another vendor you will have to adjust the report as well or it will likely throw an error. 这种方法的缺点是,如果您将数据源更改为其他供应商,则也必须调整报告,否则可能会引发错误。 When you use Cognos's own functions, Cognos automatically converts the function to the equivalent vendor-specific function for you. 当您使用Cognos自己的功能时,Cognos会自动为您将功能转换为等效的供应商特定功能。 You gain portability and maintenance at the possible expense of flexibility and power. 您获得了可移植性和维护性,但可能会牺牲灵活性和功能性。

也许是这样的吗?

SELECT CAST(DATENAME(MONTH,GETDATE()) AS VARCHAR(3)) + ' ' + CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(4)) AS mystring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 sql 中的日期类型获取月份和年份 - How do I get month and year from date type in sql 如何在“月份和年份”列中返回60套月份和年份数据? - How do I return 60 sets of month and year data in a month and year column? 如何在SSRS日期选择器(日历)控件中选择年份和月份以及日期? - How do I get to choose year and month along with date in SSRS Date Picker (Calendar) Control? 如何从给定日期获取月份名称和年份? - How to get Month Name and Year from given date? 如何使用日期列获取数字列的当前年份和上一年? - How do I get the current year and prior year of a numeric column using a date column? 如何将案例函数应用于每四年发生一次的Cognos中日期公式的a年? - How do I apply case function to a Leap Year that occurs every four years to a date formula in Cognos? 我如何与一个月和一年进行比较,而不是一个完整的日期? - How do I do a comparison with just a month and year, not a complete date? 如何在SQL中获取上一年的上个月最后一天的日期? - How do I get the date of the last day of the previous month in the previous year in SQL? 如何获取“月份年份”格式的日期? - how can i get the date in “Month Year” format? 从月份名称和年份获取sql中该月份的日期范围 - From Month Name and year Get date range of that month in sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM