How do I create a datetime from a custom format st

2019-09-05 05:48发布

I have datetime values stored in a field as strings. They are stored as strings because that's how they come across the wire and the raw values are used in other places.

For reporting, I want to convert the custom format string (yyyymmddhhmm) to a datetime field in a view. My reports will use the view and work with real datetime values. This will make queries involving date ranges much easier.

How do I perform this conversion? I created the view but can't find a way to convert the string to a datetime.

Thanks!

Update 1 -

Here's the SQL I have so far. When I try to execute, I get a conversion error "Conversion failed when converting datetime from character string."

How do I handle nulls and datetime strings that are missing the time portion (just yyyymmdd)?

SELECT  
        dbo.PV1_B.PV1_F44_C1 AS ArrivalDT,

cast(substring(dbo.PV1_B.PV1_F44_C1, 1, 8)+' '+substring(dbo.PV1_B.PV1_F44_C1, 9, 2)+':'+substring(dbo.PV1_B.PV1_F44_C1, 11, 2) as datetime) AS ArrDT,

        dbo.MSH_A.MSH_F9_C2 AS MessageType,
        dbo.PID_A.PID_F3_C1 AS PRC,
        dbo.PID_A.PID_F5_C1 AS LastName, 
        dbo.PID_A.PID_F5_C2 AS FirstName,
        dbo.PID_A.PID_F5_C3 AS MiddleInitial,
        dbo.PV1_A.PV1_F2_C1 AS Score, 
        dbo.MSH_A.MessageID AS MessageId

FROM    dbo.MSH_A
        INNER JOIN dbo.PID_A ON dbo.MSH_A.MessageID = dbo.PID_A.MessageID
        INNER JOIN dbo.PV1_A ON dbo.MSH_A.MessageID = dbo.PV1_A.MessageID
        INNER JOIN dbo.PV1_B ON dbo.MSH_A.MessageID = dbo.PV1_B.MessageID

7条回答
仙女界的扛把子
2楼-- · 2019-09-05 06:26

You can use CAST or CONVERT.

Example from the site:

G. Using CAST and CONVERT with datetime data

The following example displays the current date and time, uses CAST to change the current date and time to a character data type, and then uses CONVERT display the date and time in the ISO 8901 format.

SELECT 
   GETDATE() AS UnconvertedDateTime,
   CAST(GETDATE() AS nvarchar(30)) AS UsingCast,
   CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601;
GO

Here is the result set.

UnconvertedDateTime     UsingCast                      UsingConvertTo_ISO8601

----------------------- ------------------------------ ------------------------------

2006-04-18 09:58:04.570 Apr 18 2006 9:58AM             2006-04-18T09:58:04.570



(1 row(s) affected)
查看更多
手持菜刀,她持情操
3楼-- · 2019-09-05 06:36
DECLARE @test varchar(100) = '201104050800'
DECLARE @dt smalldatetime

SELECT @dt = SUBSTRING(@test, 5, 2) 
             + '/' + SUBSTRING(@test, 7, 2) + '/' 
             + SUBSTRING(@test, 1, 4) + ' ' + SUBSTRING(@test, 9, 2) 
             + ':' + SUBSTRING(@test, 11, 2)

SELECT @dt

Output:

2011-04-05 08:00:00

查看更多
Animai°情兽
4楼-- · 2019-09-05 06:37

Generally, you can use this code:

SELECT convert(datetime,'20110706',112)

If you need to force SQL Server to use a custom format string, use the following code:

SET DATEFORMAT ymd

SELECT convert(datetime,'20110706')
查看更多
老娘就宠你
5楼-- · 2019-09-05 06:42

A one liner:

declare @datestring varchar(255)
set @datestring = '201102281723'

select convert(datetime, stuff(stuff(@datestring,9,0,' '),12,0,':') , 112 )

Result:

2011-02-28 17:23:00.000
查看更多
霸刀☆藐视天下
6楼-- · 2019-09-05 06:43

According to here, there's no out-of-the-box CONVERT to get from your yyyymmddhhmm format to datetime.

Your strategy will be parsing the string to one of the formats provided on the documentation, then convert it.

查看更多
家丑人穷心不美
7楼-- · 2019-09-05 06:47
declare @S varchar(12)
set @S = '201107062114'

select cast(substring(@S, 1, 8)+' '+substring(@S, 9, 2)+':'+substring(@S, 11, 2) as datetime)

Result:

2011-07-06 21:14:00.000'

This first changes your date string to 20110706 21:14. Date format yyyymmdd as a string is safe to convert to datetime in SQL Server regardless of SET DATEFORMAT setting.

Edit:

declare @T table(S varchar(12))

insert into @T values('201107062114')
insert into @T values('20110706')
insert into @T values(null)

select 
  case len(S)
    when 12 then cast(substring(S, 1, 8)+' '+substring(S, 9, 2)+':'+substring(S, 11, 2) as datetime)
    when 8 then cast(S as datetime)
  end   
from @T

Result:

2011-07-06 21:14:00.000
2011-07-06 00:00:00.000
NULL
查看更多
登录 后发表回答