【发布时间】:2021-04-09 00:43:36
【问题描述】:
我想将服务月列转换为服务日期列。
例如,如果 monthofservice 列有 199012,那么我希望 servicedate 列以 1990-12-01 year-mm-date 格式显示/加载数据。我希望日期始终默认为 01。当monthofservice 列为NULL 时,我还希望servicedate 列显示NULL。使用下面的代码,当monthofservice 为NULL 时,它显示为'-'。
我确信有更好的方法来编写代码,并且当服务月列为 NULL 时也将 servicedate 显示为 NULL。非常感谢任何帮助!
SELECT monthofservice
,CASE WHEN monthofservice = '' THEN NULL ELSE CAST (concat(substring(monthofservice,1,4),+ '-', substring (MonthofService,5,2)+'-'+ '01')) AS VARCHAR (10)) AS servicedate
FROM transaction
注意:事务表中不存在 Servicedate 列,因此我使用 trasaction 中的 monthofservice 列转换为 servicedate,然后使用 SSIS 包将数据加载到其他目标表中。如果我可以通过 select 语句提取 NULL,那么我认为其他进程应该完成这项工作。
谢谢!
【问题讨论】:
-
monthofservice的数据类型是什么? -
感谢您的回复 Monthofservice 是字符串。从 char 字符串转换日期和/或时间时出现错误转换失败。另外,我希望它在 MonthofService 为 NULL 时将 servicedate 显示为 NULL
标签: sql sql-server function tsql datetime