3

How to convert yyyyMMddhh (2017092018) string to Date in SQL Server 2012?

Is it possible to do without using T-SQL to put the sentence into the INSERT clause?

INSERT INTO [dbo].[Table](SomeColumn) 
VALUES (CONVERT(DATETIME, '2017092018', 'yyyyMMddhh'));
7
  • Is it possible to do without using T-SQL to put the sentence into the INSERT clause? - I'm not sure what you're asking here... Commented Sep 20, 2017 at 14:17
  • Create a function in sql with string parameter and return Date. You can also make your function running with different custom date formats. Commented Sep 20, 2017 at 14:18
  • Is the string always in that exact format? Always the same number of characters? Commented Sep 20, 2017 at 14:18
  • Another question after your edit... you specifically say DATE twice in your question, but your example says DATETIME. What are you looking for? Commented Sep 20, 2017 at 14:20
  • Possible duplicate of Conversion failed when converting date and/or time from character string while inserting datetime Commented Sep 20, 2017 at 14:21

4 Answers 4

7

Example

Declare @S varchar(50)='2017092018'

Select convert(datetime,left(@S,8)) + convert(datetime,right(@S,2)+':00')

Returns

2017-09-20 18:00:00.000

If 2012+, I would suggest try_convert() just in case you have some unexpected values.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 from me. I didn't want to post a contradictory answer but I came up with a different approach. dateadd(hour, convert(int, right(@s, 2)), left(@s, 8))
@SeanLange Post it! That is the beauty of SO... Options
Thanks, but as I expected there is no conversion string to Date by mask =(
Old post, but still cant resist to wonder , whey sqlserver doesn't support using a standard syntax ,using format string like cast('1-1-2021', 'd-m-yyyy'). Or is it there?
@sjd Seems like a missed opportunity.
2

Alternate approach using STUFF:

DECLARE @val VARCHAR(25) = '2017092018';

SELECT CONVERT(DATETIME,STUFF(@val, 9, 0, ' ') + ':00')

This adds a space before the hour, then adds :00 for the minute value.

1 Comment

Already +1 ... Should be the accepted answer Got stuck on my initial thought.
1

Here is yet another approach to this. It is similar to what John Cappelletti posted.

Declare @S varchar(50)='2017092018'

Select dateadd(hour, convert(int, right(@s, 2)), left(@s, 8))

1 Comment

Select CAST(left(@s, 8) AS DATETIME) + RIGHT(@s,2)/24.0;
1

You could use DATETIMEFROMPARTS:

DECLARE @d NVARCHAR(10)='2017092018';

SELECT DATETIMEFROMPARTS(LEFT(@d,4),SUBSTRING(@d,5,2),SUBSTRING(@d,7,2),RIGHT(@d,2),0,0,0 ) ; 

Rextester Demo

EDIT:

Another option:

DECLARE @S varchar(10)='2017092018'
SELECT CAST(LEFT(@s, 8) AS DATETIME) + RIGHT(@s,2)/24.0;

Rextester Demo2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.