May 19, 2011 at 6:38 am
I'd like to "read" the date from the following string and add that to a datetime column:
Jane Smith 05 18 2011 mamm.pdf
There is no punctuation in the string and the month is always after the second space. What I'd like to do is pick out the 05, the 18 and the 2011, put them together, and then check to see if that is a valid date. Sometimes users are not consistent (put in three names or whatever). So check to see if we have a date. Once I have the date, I can get it into the right column.
THanks so much!!
Sam
May 19, 2011 at 6:56 am
Providing that the month is the first 2 digits and the date is 10 chars long then this should work
ISDATE(REPLACE(SUBSTRING([column],PATINDEX('%[0-9][0-9]%',@x),10),' ','/'))
and
CAST(REPLACE(SUBSTRING([column],PATINDEX('%[0-9][0-9]%',@x),10),' ','/') as datetime)
Far away is close at hand in the images of elsewhere.
Anon.
May 19, 2011 at 7:25 am
I like the patindex idea, I'll have to keep that one in the ol' toolbox.
Here's what I came up with
declare @x varchar(50) = 'Jane Smith 05 18 2011 mamm.pdf'
select cast(replace(substring(@x,
CHARINDEX(' ',@x,CHARINDEX(' ',@x,1)+1)+1,
10),' ','/') as datetime)
May 19, 2011 at 8:44 am
calvo (5/19/2011)
I like the patindex idea, I'll have to keep that one in the ol' toolbox.Here's what I came up with
declare @x varchar(50) = 'Jane Smith 05 18 2011 mamm.pdf'
select cast(replace(substring(@x,
CHARINDEX(' ',@x,CHARINDEX(' ',@x,1)+1)+1,
10),' ','/') as datetime)
Except for "Sometimes users are not consistent (put in three names or whatever). "
Far away is close at hand in the images of elsewhere.
Anon.
May 19, 2011 at 10:32 am
David Burrows (5/19/2011)
Except for "Sometimes users are not consistent (put in three names or whatever). "
Yep, I had that in mind while writing it and that's why I liked the PATINDEX method 🙂
I figured he'd run into a similar problem if someone had a number before the date in the filename (who knows). Thought I would post it just as an alternative method.
May 19, 2011 at 3:07 pm
REALLY appreciate the help!! Almost there.
Here is my statement so far:
SELECT d.ImageDetailID, d.ImageLink,
CASE WHEN
ISDATE(REPLACE(SUBSTRING(d.imageLink,PATINDEX('%[0-9][0-9]%',d.ImageLink),10),' ','/'))
THEN
CAST(REPLACE(SUBSTRING(d.ImageLink,PATINDEX('%[0-9][0-9]%',d.ImageLink),10),' ','/') as datetime) END
AS adate
FROM dbo.ImageDetail d
WHEREd.DateImage IS NULL
When I try to run get this error:
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
May 19, 2011 at 3:35 pm
figured it out! This works great:
ISDATE(REPLACE(SUBSTRING([column],PATINDEX('%[0-9][0-9]%',@x),10),' ','/')) =1
May 19, 2011 at 11:13 pm
smknox (5/19/2011)
figured it out! This works great:ISDATE(REPLACE(SUBSTRING([column],PATINDEX('%[0-9][0-9]%',@x),10),' ','/')) =1
I wouldn't put my trust in ISDATE the way you are. ISDATE returns a 1 on ANYTHING that can be converted to a date. For example...
SELECT ISDATE('2011')
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply