October 12, 2007 at 8:13 pm
Greetings to the group... I'm trying something but can't find any references on it on the web... maybe someone here can help me.
I have a text field with a value of XXXXX.XXXXXXX
Is it possible to unstring this field to select only the data after the period or only the data before the period?
Thanks in advance.
Bob
October 12, 2007 at 9:22 pm
What datatype is the column, really? Is it TEXT, VARCHAR, NVARCHAR, or ???
--Jeff Moden
Change is inevitable... Change for the better is not.
October 13, 2007 at 4:06 am
It is a char(35) fieldtype
October 13, 2007 at 10:32 am
The easiest way to split limited (up to 4 parts) period-delimited text of this nature is to use PARSENAME. Look it up in Books Online for a full explanation.
Here's a short tutorial snippet for PARSENAME...
[font="Courier New"]DECLARE @col VARCHAR(35)
    SET @Col = 'Part4.Part3.Part2.Part1'
 SELECT PARSENAME(@Col,1) AS Part1,
        PARSENAME(@Col,2) AS Part2,
        PARSENAME(@Col,3) AS Part3,
        PARSENAME(@Col,4) AS Part4[/font]
Here's how to solve your immediate problem using PARSENAME...
[font="Courier New"] SELECT PARSENAME(somecolname,1) AS Part1,
        PARSENAME(somecolname,2) AS Part2
   FROM sometable[/font]
--Jeff Moden
Change is inevitable... Change for the better is not.
October 14, 2007 at 7:26 am
Jeff,
This is exactly what I needed. Thanks very much!
Bob
October 14, 2007 at 10:11 am
Great! Thanks for the feedback, Bob!
--Jeff Moden
Change is inevitable... Change for the better is not.
October 15, 2007 at 4:19 am
Jeff,
This is really ingenious! It would never have occurred to me. However, I hope that nobody gets too enthusiastic about using it as a general way of parsing data.
[font="Courier New"]
DECLARE @col CHAR(35)
--the first parameter is supposed to be an sysname representing an 'object name'
-- and there is some testing that takes place. Just to illustrate...
--little change
SET @Col = 'Part4.Part[3].Part2.Part1'
SELECT PARSENAME(@Col,1) AS Part1,
PARSENAME(@Col,2) AS Part2,
PARSENAME(@Col,3) AS Part3,
PARSENAME(@Col,4) AS Part4
--fistfull of nulls
SET @Col = 'Part4.Part3.Part2.Part1.'
SELECT PARSENAME(@Col,1) AS Part1,
PARSENAME(@Col,2) AS Part2,
PARSENAME(@Col,3) AS Part3,
PARSENAME(@Col,4) AS Part4
--another fistfull of nulls
SET
@Col = 'Part4.Part[3].Part2.Part1'
--safer way of getting everything before the delimiter
SELECT SUBSTRING(@col,1,CHARINDEX('.', @Col+'.')-1)
--safer way of getting everything after the delimiter
SELECT STUFF ( @col, 1, CHARINDEX('.', @Col+'.'), '' )
[/font]
Best wishes,
Phil Factor
October 15, 2007 at 7:40 am
Agreed... 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
October 15, 2007 at 7:49 am
The other way to do that is to use regular expressions, especially when you get into really ugly string matching/parsing. Setting up the regex methods are damn fast too if you're willing to use CLR (same perf or better than the string functions required in T-SQL).
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 15, 2007 at 8:00 am
Heh... too bad I don't have SQL Server 2k5, yet... would be an interesting test 'cause, you're right, Regex is very fast.
The fastest option, though, would be to have correctly split data to begin with 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
October 15, 2007 at 8:33 am
Jeff Moden (10/15/2007)
Heh... too bad I don't have SQL Server 2k5, yet... would be an interesting test 'cause, you're right, Regex is very fast.The fastest option, though, would be to have correctly split data to begin with 😉
In the fairy tale land of perfectly formatted data, and foreign data that matches your specs exactly? :w00t: That would be a nice place to be.
Most days, though - I find myself myself humming the oompah lumpah song while I roll the blueberry girl down to the juicers (extract the foregin data out of fields the users have hijacked for purposes other than they were intended for).
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 15, 2007 at 9:07 am
Jeff Moden (10/15/2007)
Heh... too bad I don't have SQL Server 2k5, yet... would be an interesting test 'cause, you're right, Regex is very fast.The fastest option, though, would be to have correctly split data to begin with 😉
the DBA toolkit here on SSC has regular expressions as extended stored procedures for SQL2K; i use it all the time.
Lowell
October 15, 2007 at 9:53 am
If the text only has one period you could use the following:
DECLARE @Col VARCHAR(35)
SET @Col = 'ABCDEFGHIJKLMNOPQRST.0987654321'
SELECT left(@Col,charindex('.',@Col)-1),
right(@Col,len(@Col) - charindex('.',@Col))
October 15, 2007 at 5:07 pm
Lowell (10/15/2007)
the DBA toolkit here on SSC has regular expressions as extended stored procedures for SQL2K; i use it all the time.
Thanks for the tip, Lowell...
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2007 at 7:55 pm
I've got a problem that I can't figure out... I'm using the following as suggested to get the data to the right of the delimiter...
(SELECT STUFF(GL_SWT,1,CHARINDEX('.', @col+'.'),'')
It works fine when there are 5 characters to the left of the delimiter but when I have 6 characters to the left of the delimiter, the result set includes the delimiter.
Any ideas?
Thanks,
Bob
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply