嵌套 CAST() 函数
我正在尝试创建一个嵌套的 CAST() 函数,但正在努力解决语法问题。到目前为止,我的简化代码:
SELECT
discharge_date,
admission_date,
Patient_Uniue_ID,
CAST(CAST(Year(admission_date) AS VarChar(4)) + '-' + CAST(Month(admission_date) AS VarChar(2)) + '-' + CAST(Day(admission_date) AS VarChar(2)) AS DATE) AS 'Due_Date'
FROM SYSTEM.client_history
WHERE DateDiff('d',Due_Date, GetDate()) <= 14
尝试在每年入学周年纪念日设置投射截止日期,但我收到“无效的 SQL 格式”和“预期选项,IDENTIFIER(DATEFORMAT)”错误。有更好的方法来解决这个问题吗?
I am attempting create a nested CAST() function and am struggling with the syntax. My simplified code so far:
SELECT
discharge_date,
admission_date,
Patient_Uniue_ID,
CAST(CAST(Year(admission_date) AS VarChar(4)) + '-' + CAST(Month(admission_date) AS VarChar(2)) + '-' + CAST(Day(admission_date) AS VarChar(2)) AS DATE) AS 'Due_Date'
FROM SYSTEM.client_history
WHERE DateDiff('d',Due_Date, GetDate()) <= 14
Trying to get the casted due date to be set yearly on the anniversary of admission, but I get an "invalid SQL format" and "Option expected, IDENTIFIER(DATEFORMAT)" error. Is there a better way to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能会起作用。
下面的查询中有两个冗余列,显示了我获取今年和明年的周年纪念日期的工作。两者都增加了当前年份和入学年份之间的差异,并增加了额外的一年以获得明年。
我在 CASE 语句中使用相同的计算日期。如果今年的周年纪念日小于“未来 14 天”,则周年纪念日为明年的日期。不然就是今年了。
您可能不需要
CAST (... as DATE)
,但当我对此进行测试时,它整理了 Caché Portal 中 SQL 编辑器内的输出,显示日期而不是时间戳。您可能还需要将 14 天的测试更改为 15 天。我认为如果这种情况可能发生的话,这也适用于未来的招生。I think this might do the trick.
There are two redundant columns in the query below that show my workings to get the anniverary date for both this year and next year. Both add the difference between the current year and the admission year, with an extra year added to get next year.
I'm using the same calculated dates within a CASE statement. If the anniversary date for this year is less than '14 days in the future' then the anniversary is next year date. Otherwise it is this year.
You might not need the
CAST (... as DATE)
but it tidied up the output within the SQL editor in the Caché Portal when I tested this, displaying a date rather than a timestamp. You also might need to change the 14 day test to 15 days. I think it works for future admissions as well, if that scenario could occur.