Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

I need to compare and split the string and then save it in the detail_tb but dont know how ? Expand / Collapse
Author
Message
Posted Friday, September 28, 2012 5:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 7:39 AM
Points: 46, Visits: 111
thanks
Post #1365807
Posted Friday, September 28, 2012 5:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 7:39 AM
Points: 46, Visits: 111
Thanks alot for responding and I check its perfect but do i need to add 'XX' to my string as My string is a message from another table ?

I will do somthing like this :

Select msg+'XX' from messageIn_TB where ..........

Is this a corrrect way to do that ? I asking coz all the splitting of string is done when a INSERT TRIGGER is fired on MessageIN_Tb and I didnt tried that right now .....


My thread link:

http://www.sqlservercentral.com/Forums/Topic1365750-392-1.aspx?Update=1
Post #1365810
Posted Friday, September 28, 2012 6:18 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 9:31 AM
Points: 876, Visits: 2,392
I've seen that thread I'm not a fan of triggers on tables (personal preference) so hardly ever use them, but that doesnt mean its a bad thing.

It does need the +'XX' (or some other terminating character) as that was the only way I could get it to terminate the string and return the last data column.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1365830
Posted Monday, October 1, 2012 12:39 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:49 PM
Points: 3,648, Visits: 5,326
Here's an interesting approach using a generic string splitter (DelimitedSplit8K) that can be found here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

DECLARE @CHARACTERS TABLE (CHARS CHAR(1))
INSERT INTO @CHARACTERS VALUES
('N'), ('E'), ('M'), ('H'), ('T'), ('V'), ('L'), ('C' );

DECLARE @STRINGS TABLE (STRING VARCHAR(500));
INSERT INTO @STRINGS
SELECT '2449.555N06704.2855EM0701H071T44.098V11.764L0.372C1';

;WITH rCTE AS (
SELECT STRING, n, ItemNumber, Item
FROM @STRINGS
INNER JOIN (
SELECT n=ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),CHARS
FROM @CHARACTERS) a ON n=1
CROSS APPLY dbo.DelimitedSplit8K(STRING, CHARS)
UNION ALL
SELECT STRING, b.n+1, c.ItemNumber, c.Item
FROM rCTE b
INNER JOIN (
SELECT n=ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),CHARS
FROM @CHARACTERS) a ON a.n = b.n + 1
CROSS APPLY dbo.DelimitedSplit8K(Item, CHARS) c
WHERE b.ItemNumber <> 1
)
SELECT mvoltage=MAX(CASE WHEN n=1 THEN Item ELSE NULL END)
,mnorth=MAX(CASE WHEN n=2 THEN Item ELSE NULL END)
,meast=MAX(CASE WHEN n=3 THEN Item ELSE NULL END)
,mtime=MAX(CASE WHEN n=4 THEN Item ELSE NULL END)
,mtemperature=MAX(CASE WHEN n=5 THEN Item ELSE NULL END)
,mheight=MAX(CASE WHEN n=6 THEN Item ELSE NULL END)
,mcompraser=MAX(CASE WHEN n=7 THEN Item ELSE NULL END)
,mluman=MAX(CASE WHEN n=8 THEN Item ELSE NULL END)
FROM rCTE
WHERE ItemNumber = 1
GROUP BY STRING


Forgive me Jeff... Just having a bit of fun.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1366328
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse