如何在sql(T-sql)中构造本机日期数据类型值?
我添加了一些示例,但请提供您自己的示例.我的示例假设月份和年份以整数值存储(或随时可用),但也许您的示例将假设日期和月份(或其他)存储为文本.我看不到未来;让我感到惊讶
解决方法
为什么,输入数据作为字符串是最明显的(因此几乎不令人惊讶,抱歉)解决方案之一:
SELECT mydate = CAST([year] + RIGHT('0' + [month],2) + '01' AS datetime) /* or 'AS date' in sql Server 2008+ */ FROM ( SELECT [month] = '2',[year] = '2011' UNION ALL SELECT [month] = '03',[year] = '2011' UNION ALL SELECT [month] = '5',[year] = '2011' UNION ALL SELECT [month] = '12',[year] = '2011' UNION ALL SELECT [month] = '8',[year] = '2084' UNION ALL SELECT [month] = '1',[year] = '1940' ) x;