SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Split in Procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

asif372
Posting Yak Master

Pakistan
100 Posts

Posted - 12/26/2012 :  04:37:14  Show Profile  Reply with Quote
i want to Split my parameter values in Sql Procedure for Condition Sattement
my Parameter value is '1,2,3,4,5,6'
i want to use it like this

Create Procedure abc
@paramater Varchar
AS
BEGIN

SELECT
*
FROM
DEPARTMENT
WHERE
DEPARTMENTID IN (@Parameter)

what is the best Solution for this
Thanks in Advance

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/26/2012 :  04:59:55  Show Profile  Reply with Quote
Method1: By splitting that input parameter into separate values and then putting condition in WHERE clause

WHERE DEPARTMENTID IN (SELECT Val FROM dbo.ParseValues(@paramer, ','))

Note: Refer this link for UDF
http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html


Method2: Using LIKE operator ( Use only if your input data looks like 1,2,3,4 i.e. with out any spaces between input)

WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%'

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/26/2012 :  05:03:01  Show Profile  Reply with Quote
quote:
Originally posted by asif372

i want to Split my parameter values in Sql Procedure for Condition Sattement
my Parameter value is '1,2,3,4,5,6'
i want to use it like this

Create Procedure abc
@paramater Varchar(<length>)
AS
BEGIN

SELECT
*
FROM
DEPARTMENT
WHERE
DEPARTMENTID IN (@Parameter)

what is the best Solution for this
Thanks in Advance



Always remember to specify a length while casting to varchar

see

http://visakhm.blogspot.in/2010/02/importance-of-specifying-length-in.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

asif372
Posting Yak Master

Pakistan
100 Posts

Posted - 12/26/2012 :  05:40:44  Show Profile  Reply with Quote
Bandi thanks for your Quick responce
WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%'

This works fine if i want to add condition isnull in parameter then what is the procedure for that
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/26/2012 :  06:35:44  Show Profile  Reply with Quote
quote:
Originally posted by asif372

Bandi thanks for your Quick responce
WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%'

This works fine if i want to add condition isnull in parameter then what is the procedure for that


In that case, Method1 is correct. Add one more condition DEPARTMENT_ID IS NULL

--
Chandu
Go to Top of Page

asif372
Posting Yak Master

Pakistan
100 Posts

Posted - 12/26/2012 :  06:54:45  Show Profile  Reply with Quote
how can we add this condition for isnull in

WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%'

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/26/2012 :  07:00:10  Show Profile  Reply with Quote
quote:
Originally posted by asif372

how can we add this condition for isnull in

WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%'


WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%' OR DEPARTMENT_ID IS NULL

--
Chandu
Go to Top of Page

asif372
Posting Yak Master

Pakistan
100 Posts

Posted - 12/26/2012 :  14:40:34  Show Profile  Reply with Quote
no man i mean if my parameter is null then what will be the code
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/26/2012 :  15:04:02  Show Profile  Reply with Quote
If the parameter is null, what is the output you are expecting to get? All department_id's, or none at all, or all rows where department_id is null? If it is the last,
WHERE ',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar) + ',%' 
	OR ( DEPARTMENT_ID IS NULL AND @Parameter IS NULL)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/26/2012 :  23:12:25  Show Profile  Reply with Quote
quote:
Originally posted by asif372

no man i mean if my parameter is null then what will be the code




WHERE (',' + @Parameter + ',' LIKE '%,' + CAST(DEPARTMENT_ID AS varchar(10)) + ',%' OR DEPARTMENT_ID IS NULL OR @Parameter IS NULL)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000