C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Comma Seprated Values for numbers


Posted Date: 06 Oct 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Prafulla S Shimpi       Member Level: Gold
Rating:     Points: 10



Sometimes, we have to pass a string of numbers separated by comma to a stored procedures / functions, and it doesn't work, throws an error like "cannot convert NVARCHAR value to interger"

e.g. string like '1001','1002','1003','1004','1005'

The problem happens here is: it replaces your comma separted list as below:

a. if it only one number, it converts to --> '1001'
b. but if it contains more than one number, then, it converts it like --> ''1001','1002','1003'', so it becomes a string, and not able to convert it to integer value.

To sort out this problem, just try this:

1. write this function as User defined function in SQL.
2. while writing query and using comma separated list e.g. say strProgIds, write/use like this:

select ufn_GetTableFromCSV(strProgIds)


It will give you comma separated values for each of the ProgIds.



--User Defined Function

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



--Function to get a table with giving CSV as input
ALTER function [dbo].[ufn_GetTableFromCSV](@csvstr varchar(6000))
Returns @csvTable table (value varchar(500))
as
begin
DECLARE @start1 INTEGER
DECLARE @end1 INTEGER
Declare @ID varchar(500)

declare @flag bit

set @flag = 1
SET @start1 = 1
SET @end1 = 0


if(@csvstr <>'')
begin
--WHILE(@start1 < LEN(@csvstr))
WHILE(@flag = 1)

BEGIN

set @end1 = charindex(',', @csvstr, @start1)
if(@end1 != 0)
begin
SET @ID = SUBSTring(@csvstr, @start1, @end1 - @start1)

IF(@ID <> '')
BEGIN
insert @csvTable select @ID
END

SET @start1 = @end1 + 1
end
else
begin
set @flag = 0
end
END
end
--to get last substring part after the last ,
SET @ID = SUBSTring(@csvstr, @start1, len(@csvstr) + 1 - @start1)

IF(@ID <> '')
BEGIN
insert @csvTable select @ID
END

return
end








Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SQL Stored procedures  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to get the columns in tha table?
Previous Resource: Indexes on a Table
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

online optimum rewards

Contact Us    Privacy Policy    Terms Of Use