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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Conversion from NVarchar To Int
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 07/22/2012 :  13:36:33  Show Profile  Reply with Quote
In my procedure value comes in character.
like - '251064,251065,251066'
type of my field is bigint

now when i try like this

ALTER PROCEDURE [dbo].[DelScratch]
(@ScratchNo Nvarchar (100))
AS
BEGIN
--@ScratchNo = '251064,251065,251066'
Delete from scratchcard Where ScratchNo in (@ScratchNo)
Delete from ScratchMast Where ScratchNo in (@ScratchNo)
END

it gives error like
Error converting data type varchar to bigint.

How can i convert it in sql server


VB6/ASP.NET
------------------------
http://www.nehasoftec.com

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/22/2012 :  14:11:17  Show Profile  Reply with Quote
SQL Server does not let you use a variable in the manner you are trying to use in the query. Your choices are:

a) To split the comma-separated string into individual tokens using a string splitter function into a (virtual) table and then join with that table. There are string splitter functions available on the web and in SQL Team archives - for example Fig 21 in here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

b) Change your where clause to this:
WHERE ','+@ScratchNo+',' LIKE '%,'+CAST(ScratchNo AS NVARCHAR(32))+',%'
The second approach is simpler, but it may be less efficient because of the non-sargable predicate.
Go to Top of Page

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 07/22/2012 :  14:16:32  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

SQL Server does not let you use a variable in the manner you are trying to use in the query. Your choices are:

a) To split the comma-separated string into individual tokens using a string splitter function into a (virtual) table and then join with that table. There are string splitter functions available on the web and in SQL Team archives - for example Fig 21 in here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

b) Change your where clause to this:
WHERE ','+@ScratchNo+',' LIKE '%,'+CAST(ScratchNo AS NVARCHAR(32))+',%'
The second approach is simpler, but it may be less efficient because of the non-sargable predicate.



Thanx

VB6/ASP.NET
------------------------
http://www.nehasoftec.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.06 seconds. Powered By: Snitz Forums 2000