简体   繁体   English

在Cognos report Studio中将具有整数数据类型的列转换为日期

[英]Convert a column that has a data type of integer into date in Cognos report studio

I have a column that appear like this and the data type is integer. 我有一列看起来像这样,数据类型为整数。 I get that data from AS400 server that's why it uses integer data type. 我从AS400服务器获得了数据,这就是为什么它使用整数数据类型的原因。 The date format is represent as YYYYMMDD 日期格式表示为YYYYMMDD

在此处输入图片说明

在此处输入图片说明

In report studio, I created a data item that would convert this integer column to date time. 在Report Studio中,我创建了一个数据项,该数据项会将整数列转换为日期时间。 But it failed. 但是失败了。

在此处输入图片说明

I have tried lots of different approach but none of these worked. 我尝试了许多不同的方法,但这些方法均无效。

cast([WCPDOD], 'YYYYMMDD') 演员表([WCPDOD],'YYYYMMDD')

cast([WCPDOD], date) UDA-SQL-0219 The function "to_date" is being used for local processing but is not available as a built-in function, or at least one of its parameters is not supported.RSV-SRV-0042 强制转换([WCPDOD],日期)UDA-SQL-0219函数“ to_date”正在用于本地处理,但不能用作内置函数,或者至少不支持其参数之一。RSV-SRV- 0042

cast([WCPDOD], YYYY-MM-DD) 演员表([WCPDOD],YYYY-MM-DD)

cast([WCPDOD], datetime) 演员表([WCPDOD],日期时间)

cast_timestamp([WCPDOD], datetime) cast_timestamp([WCPDOD],日期时间)

cast_timestamp([WCPDOD], date) cast_timestamp([WCPDOD],日期)

cast_integer([WCPDOD], date) cast_integer([WCPDOD],日期)

Can someone help me with this? 有人可以帮我弄这个吗? My goal is to get this 20150729 into this 07/29/2015 at least 我的目标是至少将此20150729纳入此07/29/2015

First, cast your 10-digit integer into a string: 首先,将10位整数转换为字符串:

Data Item2 数据项2

cast([Data Item1],varchar(10))

Next, use substring to extract out the date components and build a date string: 接下来,使用子字符串提取日期成分并构建日期字符串:

Data Item3 数据项3

substring([Data Item2],1,4) + '-' + substring([Data Item2],5,2) + '-' + substring([Data Item2],7,2)

Lastly, convert the resulting string to date format: 最后,将结果字符串转换为日期格式:

Data Item4 数据项4

cast([Data Item3],date)

Of course, this can all be done in a single expression but I broke it out here for clarity. 当然,所有这些都可以在一个表达式中完成,但是为了清楚起见,在这里我将其分解。

In SQL Server, you can convert the integer field to varchar and then to a date and then use the date style 101 to achieve your desired format: 在SQL Server中,可以将整数字段转换为varchar,然后转换为日期,然后使用日期样式101来实现所需的格式:

DECLARE @datevalue int = '20150729';

SELECT convert(varchar(10),cast(cast(@datevalue AS varchar(10)) as date), 101);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM