T-SQL datetime conversion(T-SQL 日期时间转换)
问题描述
我正在使用第 3 方数据库,该数据库(无论出于何种原因)内部以 yyyymm 格式存储日期,例如 201212 for Dec 2012 作为 INT.
I'm working with a 3rd party database, which (for whatever reason) internal stores a date in the format yyyymm eg 201212 for Dec 2012 as an INT.
是否有一种简单的方法可以将其转换为 SQL Server DateTime 类型?
Is there a simple way that I may convert this to a SQL Server DateTime type?
我尝试将上述内容手动转换为长度为 6 的字符序列,将其与 '01' 连接以获取给定月份的开始,如下所示:
I've tried manually converting the above to a char seq of length 6, concatenating this with '01' to get the start of a given month like so:
(CONVERT(char(6), Period) + '01')`
然后将其包装在 datetime 转换中:
then wrapping this in a datetime conversion:
CONVERT(datetime, (CONVERT(char(6), Period) + '01'))
但这似乎抛出;
从字符串转换日期和/或时间时转换失败
谁能解释一下我做错了什么?
Can anyone shed any light on what I'm doing wrong?
谢谢!:)
推荐答案
您需要使用正确的参数.这是 msdn 图表 http://msdn.microsoft.com/en-us/library/ms187928.aspx
You need to use the correct parameter. Here's the msdn chart http://msdn.microsoft.com/en-us/library/ms187928.aspx
就您而言,我认为您需要附加01"并使用它
In your case, i believe you need to append the '01' and use this
declare @val VARCHAR(8)
SET @val = '201212'
set @val = @val + '01'
print CONVERT(DATETIME, @val, 12)
12 是 ISO 格式,将采用 yymmdd 或 yyyymmdd
12 is the ISO format and will take either yymmdd or yyyymmdd
这篇关于T-SQL 日期时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:T-SQL 日期时间转换
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
