April 20, 2008 at 4:05 am
Hi all
I would be grateful if someone please help me to find date and time from a given string .
the string value is as below
'20081011192921703'
many Thanks
Rad
April 20, 2008 at 9:37 am
Just checking... you mean you have no record layout information for this? We can guess that the first 8 digits are in the yyyymmdd format, and we can guess that the other information might in the format of hhmissmmm, but we wouldn't know for sure.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 21, 2008 at 3:02 am
Hi Jeff
Thanks for the reply..
Yes you are right in your analysis that first 8 digit are are date and the rest is the time. I want to extract this information in a proper date & time format.
Rad
April 21, 2008 at 3:25 am
Here are a couple of ways...
declare @x varchar(30)
set @x = '20081011192921703'
select cast(substring(@x, 1, 8) + ' ' + substring(@x, 9, 2) + ':' + substring(@x, 11, 2)
+ ':' + substring(@x, 13, 2) + '.' + substring(@x, 15, 3) as datetime) as dtm
select cast(stuff(stuff(stuff(stuff(@x, 9, 0, ' '), 12, 0, ':'), 15, 0, ':'), 18, 0, '.') as datetime) as dtm
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
April 21, 2008 at 3:55 am
Brilliant mate...
thanks a ton...
April 21, 2008 at 4:54 am
This uses explicit conversion, rather than implicit:
DECLARE @CharDate CHAR(23)
SET @CharDate = '20081011192921703'
SET @CharDate = STUFF(STUFF(STUFF(STUFF(STUFF(STUFF(@CharDate, 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':'), 20, 0, ':')
SELECT @CharDate
SELECT CONVERT(DATETIME, @CharDate, 121)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 21, 2008 at 5:30 am
Chris used the method I would have. Ryan's is also a viable solution.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 21, 2008 at 5:38 am
This uses explicit conversion, rather than implicit[/code]
Chris
I don't see any implicit conversion. Are you suggesting that what I posted is implicit?
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
April 21, 2008 at 7:38 am
RyanRandall (4/21/2008)
This uses explicit conversion, rather than implicit[/code]
Chris
I don't see any implicit conversion. Are you suggesting that what I posted is implicit?
Hi Ryan
Not at all - CAST() is explicit conversion. Apologies for the confusion.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 21, 2008 at 7:58 am
Chris Morris (4/21/2008)
RyanRandall (4/21/2008)
This uses explicit conversion, rather than implicit[/code]
Chris
I don't see any implicit conversion. Are you suggesting that what I posted is implicit?
Hi Ryan
Not at all - CAST() is explicit conversion. Apologies for the confusion.
Cheers
ChrisM
Thanks for clarifying 🙂
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply