SQL Server:将((int)year,(int)month,(int)day)转换为Datetime [英] SQL Server: convert ((int)year,(int)month,(int)day) to Datetime

查看:138
本文介绍了SQL Server:将((int)year,(int)month,(int)day)转换为Datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用T-SQL创建日期

我有一个数据表存储每个年,月和日价值为ints:

I've a data table that stores each year, month and day value as ints:

year | month | day
2009 |   1   |  1 
2008 |  12   |  2
2007 |   5   |  5

我需要将其转换为datetime值,因为我需要在操作之间的datetime中使用它。我该怎么做?

I need to convert it to datetime value, because I need to use it in a datetime between operation. How could I do this?

推荐答案

为了独立于语言和区域设置,您应该使用ISO 8601 YYYYMMDD 格式 - 这将适用于任何具有任何语言和区域设置的SQL Server系统:

In order to be independent of the language and locale settings, you should use the ISO 8601 YYYYMMDD format - this will work on any SQL Server system with any language and regional setting in effect:

SELECT
   CAST(
      CAST(year AS VARCHAR(4)) +
      RIGHT('0' + CAST(month AS VARCHAR(2)), 2) +
      RIGHT('0' + CAST(day AS VARCHAR(2)), 2) 
   AS DATETIME)

这篇关于SQL Server:将((int)year,(int)month,(int)day)转换为Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆