December 31, 2013 at 2:07 pm
Hi folks, I need to know if it's possible, with T-SQL, to extract the year stamp from strings as variable as the following:
iMac (21.5-inch, Late 2013)
iMac (27-inch, Late 2013)
20-inch/17-inch iMac (Early 2006)
17-inch iMac (Mid 2006), 1.83 GHz Intel Core Duo
iMac (20-inch + 17-inch, Late 2006)
iMac (17-inch Late 2006 CD)
The year can appear anywhere in the string and is not the only string containing numbers.....
--Quote me
December 31, 2013 at 2:37 pm
Not really extracting, but as long as the year numbers can't appear in any other part of the string and the number of years is finite...
create table #stuff (
badstrings varchar(255) null)
insert #stuff
select 'iMac (21.5-inch, Late 2013)'
union
select 'iMac (27-inch, Late 2013)'
union
select '20-inch/17-inch iMac (Early 2006)'
union
select '17-inch iMac (Mid 2006), 1.83 GHz Intel Core Duo'
union
select 'iMac (20-inch + 17-inch, Late 2006)'
union
select 'iMac (17-inch Late 2006 CD)'
select *, case when badstrings like '%1999%' then '1999'
when badstrings like '%2000%' then '2000'
when badstrings like '%2001%' then '2001'
when badstrings like '%2002%' then '2002'
when badstrings like '%2003%' then '2003'
when badstrings like '%2004%' then '2004'
when badstrings like '%2005%' then '2005'
when badstrings like '%2006%' then '2006'
when badstrings like '%2007%' then '2007'
when badstrings like '%2008%' then '2008'
when badstrings like '%2009%' then '2009'
when badstrings like '%2010%' then '2010'
when badstrings like '%2011%' then '2011'
when badstrings like '%2012%' then '2012'
when badstrings like '%2013%' then '2013'
else 'not found'
end
from #stuff
When in doubt, reframe the problem...
December 31, 2013 at 2:53 pm
I didn't think about the simple approach. I immediately conjured the problem to involve complex nesting of substring and pathindex functions. Thanks David and Happy New Year!
--Quote me
December 31, 2013 at 2:56 pm
That's where I went too and realized I couldn't solve that. Happy New Year to you too!
January 1, 2014 at 8:31 am
This seems to work fine, unless you have other 4 digits number in your string.
If you have any questions on how it works or why did I used something, feel free to ask.
--Using David's sample data
SELECT badstrings, CASE WHEN PATINDEX( '%[1-2][0-9][0-9][0-9][^0-9]%',badstrings+'.') > 0
THEN SUBSTRING( badstrings, PATINDEX( '%[1-2][0-9][0-9][0-9][^0-9]%',badstrings+'.'), 4)
ELSE '' END
FROM #stuff
Happy New Year 😀
January 1, 2014 at 11:16 am
polkadot (12/31/2013)
Hi folks, I need to know if it's possible, with T-SQL, to extract the year stamp from strings as variable as the following:iMac (21.5-inch, Late 2013)
iMac (27-inch, Late 2013)
20-inch/17-inch iMac (Early 2006)
17-inch iMac (Mid 2006), 1.83 GHz Intel Core Duo
iMac (20-inch + 17-inch, Late 2006)
iMac (17-inch Late 2006 CD)
The year can appear anywhere in the string and is not the only string containing numbers.....
For better accuracy, lemme ask... will the year ALWAYS be preceded by the word Early, Mid, or Late?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply