-4

我有这个查询,它提取了很久以前在数据库中编程的日期数值,并将其显示为 19930215.202300000。因此,对于三个不同的列,我到目前为止所做的是将字符串转换为日期和时间

现有查询:

 select SerialNum as [Serial Number],ts_sitename As Site,(case m.Scratched
 when 0 then 'Live'
 when 1 then 'Free'
 END) as Status,  Note as Comment, (case Destroyed when 0 then 'NO'
 when 1 then 'YES' END) as [Destroyed], 
 SUBSTRING(cast(EffectiveDate as char), 1, 8) AS [Effective Date Added], 
 SUBSTRING(cast(EffectiveDate as char), 10, 6) AS [Effective Time Added], 
 SUBSTRING(cast(ScratchedDate as char), 1, 8) AS [Scratched Date], 
 SUBSTRING(cast(ScratchedDate as char), 10, 6) AS [Scratched Time], 
 SUBSTRING(cast(ChangedPurgeDate as char), 1, 8) AS [Purge Date], 
 SUBSTRING(cast(ChangedPurgeDate as char), 10, 6) AS [Purge Time], 
 (select fl_filename from TheFiles_tab where mg_filenum = fl_filenum) as [Dataset],
 (select hs_hostname from TheHosts_tab where mg_hostnum = hs_hostnum) as [Host], 
 (select UserCode from [User] where mg_usernum = UserId) as [UserCode]
 from ((Media m left join MediaGenT g on m.MediaId = g.mg_medianum) 
 join TheSites_tab s on m.SiteId = s.ts_sitenum) 
 join Note n on m.NoteId = n.NoteId;

所以这显示有效日期为 20120327

但是所需的输出是2012-03-27

当前显示有效时间为213100

所需输出为 21:31:00

.. 其他四个日期和时间列也是如此。

请注意,我使用 SQL Server 2008 来运行我的查询。

任何人都可以对我的代码进行任何编辑以获得所需的输出,我只是不擅长字符串操作吗?我很想立即对其进行测试。

4

2 回答 2

2

也许您可以使用它来集成到您的代码中。

declare @d decimal(20,9)
declare @yy varchar(4)
declare @mm varchar(2)
declare @dd varchar(2)
declare @hh varchar(2)
declare @mi varchar(2)
declare @tt varchar(2)
set @d = 19930215.202300000
set @yy = CAST(left(@d,4) as varchar)
set @mm = CAST(substring(cast(@d as varchar),5,2) as varchar)
set @dd = CAST(substring(cast(@d as varchar),7,2) as varchar)
set @hh = CAST(substring(cast(@d-floor(@d) as varchar),3,2) as varchar)
set @mi = CAST(substring(cast(@d-floor(@d) as varchar),5,2) as varchar)
set @tt = case when cast(@hh as int) > 12 then 'PM' else 'AM' end
select @d,@yy,@mm,@dd,@hh,@mi,@tt

这会输出如下内容:

19930215.202300000,1993,02,15,20,23,PM
于 2013-08-15T16:13:05.193 回答
2

首先将您的值转换为 DateTime,然后将其转换为 120 格式的 varchar(10)。

SELECT 
    [Serial Number] = SerialNum 
 ,  [Site]          = ts_sitename
 ,  [Status]        = 
    (
        CASE m.Scratched
        WHEN 0 THEN 'Live'
        WHEN 1 THEN 'Free'
        END
    ) 
 ,  [Comment]   =   Note
 ,  [Destroyed] =
    (
        CASE Destroyed 
        WHEN 0 THEN 'NO'
        WHEN 1 THEN 'YES' 
        END
    ) 
,   [Effective Date Added]  = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, EffectiveDate),       1, 8)), 120)
,   [Effective Time Added]  = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, EffectiveDate),      10, 6)), 120)
,   [Scratched Date]        = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, ScratchedDate),       1, 8)), 120)
,   [Scratched Time]        = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, ScratchedDate),      10, 6)), 120)
,   [Purge Date]            = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, ChangedPurgeDate),    1, 8)), 120)
,   [Purge Time]            = CONVERT(VARCHAR(CONVERT(DATETIME,SUBSTRING(CONVERT(CHAR, ChangedPurgeDate),   10, 6)), 120)
,   [Dataset]               = (SELECT fl_filename from TheFiles_tab where mg_filenum = fl_filenum) 
,   [Host]                  = (SELECT hs_hostname from TheHosts_tab where mg_hostnum = hs_hostnum) 
,   [UserCode]              = (SELECT UserCode from [User] where mg_usernum = UserId) 
FROM ((Media m left join MediaGenT g on m.MediaId = g.mg_medianum) 
JOIN TheSites_tab s on m.SiteId = s.ts_sitenum) 
JOIN Note n on m.NoteId = n.NoteId;
于 2013-08-15T16:17:25.243 回答