March 19, 2015 at 12:11 pm
Hi
I have a field which looks like "LastName, FirstName (DOB: 01/01/1900)"
How do I get the "01/01/1900" between ":" and ")"
Thanks in Advance
Joe
March 19, 2015 at 12:30 pm
jbalbo (3/19/2015)
HiI have a field which looks like "LastName, FirstName (DOB: 01/01/1900)"
How do I get the "01/01/1900" between ":" and ")"
Thanks in Advance
Joe
I'd try something like:
DECLARE @TestString VARCHAR(50)
SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'
SELECT SUBSTRING(@TestString, CHARINDEX(':', @TestString) + 2, CHARINDEX(')', @TestString) - CHARINDEX(':', @TestString) - 2)
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
March 19, 2015 at 12:49 pm
That works great, Thank You....
And if I looked at the table "Just a bit" closer I would have seen that DOB was there... lol
But I did learn something new!
Thanks Again
March 19, 2015 at 1:18 pm
These problems are fun. If the data format is always the same (##/##/####) you could also do this:
DECLARE @TestString VARCHAR(50)
SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'
SELECT SUBSTRING(@TestString, PATINDEX('%[0-1][1-9]/%', @TestString),10);
-- Itzik Ben-Gan 2001
March 19, 2015 at 1:36 pm
Alan.B (3/19/2015)
These problems are fun. If the data format is always the same (##/##/####) you could also do this:
DECLARE @TestString VARCHAR(50)
SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'
SELECT SUBSTRING(@TestString, PATINDEX('%[0-1][1-9]/%', @TestString),10);
I agree. It can be fun to see what other ways people come up with as an answer to string manipulation in sql.
Here is one:
select left(ltrim(PARSENAME(replace(@TestString, ':', '.'), 1)), 10)
--edit--
typo...again
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 19, 2015 at 1:38 pm
I will definitely be able to use this for something, but I did find a problem with the formatting,
I got en error because one record had parenthesis in the name i.e.. (John Smith(aka jack) DOB: 01/01/1900)
Any ideas if not in the same structure?
March 19, 2015 at 1:50 pm
jbalbo (3/19/2015)
I will definitely be able to use this for something, but I did find a problem with the formatting,I got en error because one record had parenthesis in the name i.e.. (John Smith(aka jack) DOB: 01/01/1900)
Any ideas if not in the same structure?
AS long as there's no ":" in the string, this revised code should work:
DECLARE @TestString VARCHAR(50)
SET @TestString = 'LastName(s), FirstName(s) (DOB: 01/01/1900)'
SELECT SUBSTRING(@TestString, CHARINDEX(':', @TestString) + 2, CHARINDEX(')', @TestString, CHARINDEX(':', @TestString)) - CHARINDEX(':', @TestString) - 2)
This should work even if there is one or more ":" in the names:
DECLARE @TestString VARCHAR(50)
SET @TestString = 'LastName(s), FirstName(s) (DOB: 01/01/1900)'
SELECT SUBSTRING(@TestString, CHARINDEX('DOB:', @TestString) + 5, CHARINDEX(')', @TestString, CHARINDEX('DOB:', @TestString)) - CHARINDEX('DOB:', @TestString) - 5)
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply