TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar

By: Dave Emailed: 1586 times Printed: 2040 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

I have been frustrated with the 8000 limit set by the varchar data type in MSSQL. For procedures that required text length more than 8000 character limit, MSSQL offers the TEXT datatype. However there are not as many functions that are supported for TEXT datatype. For example if you would like to split the data string which is seperated by a delimitter, then it is not easy to split the text.

The code below can be used to do just that. It really breaks the 8000 character limit

CREATE FUNCTION RowParser 
( 
@Text TEXT, 
@Separator VARCHAR(3)  = ',' 
) 
RETURNS TABLE 
AS 
RETURN 
( 
SELECT n, 
  SUBSTRING(@Text collate database_default, n,  
  CASE SIGN(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n) 
   WHEN -1 THEN  
     CASE PATINDEX('%,%', SUBSTRING(@Text collate database_default, n, ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n))) 
      WHEN 0 THEN DATALENGTH(@Text)-n+1 
      ELSE PATINDEX('%,%', SUBSTRING(@Text collate database_default, n, ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n))) -1 
     END 
   ELSE ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n) 
   END) AS Data 
       , DATALENGTH(@Text)/n  AS Position 
, CASE SIGN(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n) 
   WHEN -1 THEN  
     CASE PATINDEX('%,%', SUBSTRING(@Text collate database_default, n, ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n))) 
      WHEN 0 THEN DATALENGTH(@Text)-n+1 
      ELSE PATINDEX('%,%', SUBSTRING(@Text collate database_default, n, ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n))) -1 
     END 
   ELSE ABS(CHARINDEX(@Separator collate database_default, @Text collate database_default,n)-n) 
   END AS NumberStep 
FROM Numbers 
WHERE n BETWEEN 0 AND (DATALENGTH(@Text) - DATALENGTH(@Separator)) 
AND ((SUBSTRING(@Text collate database_default,n-DATALENGTH(@Separator),DATALENGTH(@Separator)) = @Separator AND n > 1) OR n = 1) 
) 
GO

JDBC Home | All JDBC Tutorials | Latest JDBC Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

HI,

The function is throwing an error saying that "Invalid object name 'Numbers'." Please have a look at this and let me know the solution. Its really urgent for me to get this function working. Thanks in advance.


View Tutorial          By: Vamshi at 2010-08-31 05:04:03
2. View Comment

'Numbers' is the name of his table, you need to substitute with the name of your table's name instead.

View Tutorial          By: Amir at 2010-09-15 10:59:20

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dave
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar

More Tutorials in JDBC
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
Import TEXT to TABLE in MSSQL
What is Referential Integrity in databases?
Handling CSV in Stored Procedures
setSavepoint and releaseSavepoint Example in Java
Calling a Stored Procedure from JDBC in Java
java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components
SELECT Statements
WHERE Clauses in SQL
Joins example in SQL
Common SQL Commands
Result Sets, Cursors and Transactions in SQL
Stored Procedures example in SQL

More Latest News
Most Viewed Articles (in JDBC )
A simple JDBC application sample code
How connection pooling works in Java and JDBC
JDBC Components
Using the DriverManager Class vs Using a DataSource Object for a connection
Using JDBC to extract data from a database and output to an XML document
Using JDBC to connect to MySQL from Java Program
JDBC Architecture
WHERE Clauses in SQL
Stored Procedures example in SQL
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components
Common SQL Commands
Creating Database Tables Using ANT
PreparedStatement Example in Java
setSavepoint and releaseSavepoint Example in Java
Most Emailed Articles (in JDBC)
The Structure of JDBC
A simple JDBC application sample code
Using Transactions in JDBC
JDBC Components
Using JDBC to extract data from a database and output to an XML document
What is the ACID principal?
Using JDBC to connect to MySQL from Java Program
How connection pooling works in Java and JDBC
JDBC and Tomcat context settings
PreparedStatement Example in Java
Data Access Technologies in Java
Result Sets, Cursors and Transactions in SQL
Handling CSV in Stored Procedures
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components