Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2012-04-06 : 03:26:25
|
i have a table[table1] with a field summarywhich has data as1-AB-XY2-OP-AB1-AB-XY1-MN-ZZ-AB1-OP-AB2-AB-XY1-MN-ZZ-AB1-OP-AB1-MN-ZZ-ABwith the above data hardcoded in the below sample i get the desired result(No column name) (No column name)1-AB 71-MN 31-OP 21-XY 21-ZZ 32-AB 22-OP 12-XY 1/*CREATE FUNCTION dbo.split(@delimited NVARCHAR(MAX),@delimiter NVARCHAR(100)) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))ASBEGINDECLARE @xml XMLSET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'INSERT INTO @t(val)SELECT r.value('.','varchar(MAX)') as itemFROM @xml.nodes('/t') as records(r)RETURNEND*/DECLARE @TAB TABLE (SUMMARY VARCHAR(100))INSERT INTO @TABSELECT '1-AB-XY'UNION ALL SELECT '2-OP-AB'UNION ALL SELECT '1-AB-XY'UNION ALL SELECT '1-MN-ZZ-AB'UNION ALL SELECT '1-OP-AB'UNION ALL SELECT '2-AB-XY'UNION ALL SELECT '1-MN-ZZ-AB'UNION ALL SELECT '1-OP-AB'UNION ALL SELECT '1-MN-ZZ-AB'SELECT id + '-' + val, count(1)FROM (SELECT LEFT(SUMMARY, CHARINDEX('-', SUMMARY,1)-1) AS id, valFROM @TAB CROSS APPLY dbo.split(SUBSTRING(SUMMARY,CHARINDEX('-', SUMMARY,1)+1 ,100), '-')) TABGROUP BY id + '-' + valbut instead if i directly need to get the value from my existing column, so replacing TAB with mytablename i get errorMsg 536, Level 16, State 5, Line 1Invalid length parameter passed to the SUBSTRING function. |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-04-06 : 11:40:16
|
What values are you using for the SUBSTRING functions?=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-06 : 15:31:37
|
that may be because there are values without - present in your tablewhat does below return?select count(*) from table where charindex('-',field)=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2012-04-07 : 02:21:06
|
quote: Originally posted by visakh16 that may be because there are values without - present in your tablewhat does below return?select count(*) from table where charindex('-',field)=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
returns 1(No column name)1 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-08 : 18:14:26
|
so that does mean there's a value without - present and that will break with CHARINDEXtry likeSELECT id + '-' + val, count(1)FROM (SELECT LEFT(SUMMARY+'-', CHARINDEX('-', SUMMARY+'-',1)-1) AS id, valFROM @TAB CROSS APPLY dbo.split(SUBSTRING(SUMMARY+'-',CHARINDEX('-', SUMMARY+'-',1)+1 ,100), '-')) TABGROUP BY id + '-' + val ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2012-04-09 : 06:03:37
|
quote: Originally posted by visakh16 so that does mean there's a value without - present and that will break with CHARINDEXtry likeSELECT id + '-' + val, count(1)FROM (SELECT LEFT(SUMMARY+'-', CHARINDEX('-', SUMMARY+'-',1)-1) AS id, valFROM @TAB CROSS APPLY dbo.split(SUBSTRING(SUMMARY+'-',CHARINDEX('-', SUMMARY+'-',1)+1 ,100), '-')) TABGROUP BY id + '-' + val ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/thanks the query is working in sql but i dont think its helpful for me as i get cross apply sql construct or statement is not supported
when i am using this query in XSD file |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-09 : 11:17:13
|
hhmm? why do you need to use it inside xsd?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|