Sql 如何将整数转换为数据和时间格式

Sql 如何将整数转换为数据和时间格式,sql,sql-server,datetime,select,sql-timestamp,Sql,Sql Server,Datetime,Select,Sql Timestamp,表tblpress中的两列 需要选择以下格式: 07-03-2016 12:09:49 07-03-2016 13:34 27 或 我认为CAST/CONVERT将帮助您: SELECT CAST('20160307' AS date), CAST(STUFF(STUFF('120949',3,0,':'),6,0,':') AS time) 并转换为输出: SELECT CONVERT(varchar(20),NormalDate,105) OutDate, -- Italian

表tblpress中的两列

需要选择以下格式:

07-03-2016 12:09:49
07-03-2016 13:34 27


我认为
CAST/CONVERT
将帮助您:

SELECT
  CAST('20160307' AS date),
  CAST(STUFF(STUFF('120949',3,0,':'),6,0,':') AS time)
并转换为输出:

SELECT
  CONVERT(varchar(20),NormalDate,105) OutDate, -- Italian style
  CONVERT(varchar(20),NormalTime,108) OutTime -- hh:mi:ss
FROM
  (
    SELECT
      CAST([Date] AS date) NormalDate,
      CAST(STUFF(STUFF([Time],3,0,':'),6,0,':') AS time) NormalTime
    FROM YourTable
  ) q

你可以用

你可以在下面试试

select format(cast([Date] as date),'dd-MMMM-yyyy') as [Date],
TIMEFROMPARTS(LEFT([Time],2), SUBSTRING([Time],3,2), RIGHT([Time],2), 0,0) as [Time]

最好的方法是创建一个函数:

create FUNCTION [dbo].[udfGetDateTimeFromInteger]
(
   @intDate int,
   @intTime int
)
RETURNS datetime
AS BEGIN
-- Declare the return variable here
DECLARE @DT_datetime datetime = NULL,
  @str_date varchar(11),
  @str_time varchar(8)
if(@intDate is not null and @intDate > 0)
begin
  select @str_date = CAST( cast(@intDate as varchar(8)) AS date)
  if @intTime=0
     select @str_time ='000000'
  else
     select @str_time = right('0'+CONVERT(varchar(11),@intTime),6)
  select @str_time = 
   SUBSTRING(@str_time,1,2)+':'+SUBSTRING(@str_time,3,2)+':'+SUBSTRING(@str_time,5,2)

  select @DT_datetime = CAST(@str_date+' '+@str_time as datetime)
 end
 -- Return the result of the function
 RETURN @DT_datetime

 END
然后调用它,如选择:

declare @next_run_date int, @next_run_time int
select @next_run_date = 20160307
select @next_run_time = 130949

SELECT  @next_run_date inputdate,
                @next_run_time inputtime,
                dbo.udfGetDateTimeFromInteger(@next_run_date, @next_run_time) outputdatetime
输出如下所示:

inputdate   inputtime   outputdatetime
20160307    130949      2016-03-07 13:09:49.000

你说这些是数字,对吗?您可以使用datetimefromparts(或datetime2fromparts)。即:

请注意,这样命名字段并存储日期和时间是个坏主意

我后来注意到它是char字段:

select 
  cast([date] as datetime) +
  cast(stuff(stuff([time],5,0,':'),3,0,':') as datetime)
from tblpress;

您使用什么数据库?我使用ms sql server获得所需的输出格式
Date
Time
中的数据类型是什么
Int
?输出+1的日期和时间字符,但我认为这不是一种稳健的“最佳”方式?为什么这是最好的?即使创建一个函数可能是好的,但这不是最好的函数,对吗?@CetinBasoz完全同意我从未说过这个函数是最好的:)。我的意思是减少实际的代码过载
declare @next_run_date int, @next_run_time int
select @next_run_date = 20160307
select @next_run_time = 130949

SELECT  @next_run_date inputdate,
                @next_run_time inputtime,
                dbo.udfGetDateTimeFromInteger(@next_run_date, @next_run_time) outputdatetime
inputdate   inputtime   outputdatetime
20160307    130949      2016-03-07 13:09:49.000
select 
 datetimefromparts(
  [date]/10000,
  [date]%10000/100,
  [date]%100,
  [time]/10000,
  [time]%10000/100,
  [time]%100,0)
from tblpress;
select 
  cast([date] as datetime) +
  cast(stuff(stuff([time],5,0,':'),3,0,':') as datetime)
from tblpress;