Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server Newbies
»
Converting from Varchar to bigint
Converting from Varchar to bigint
Rate Topic
Display Mode
Topic Options
Author
Message
Vinay Theethira
Vinay Theethira
Posted Tuesday, May 05, 2009 8:41 AM
SSC Rookie
Group: General Forum Members
Last Login: Today @ 5:26 AM
Points: 29,
Visits: 105
Hi All,
Here i am having a SP where i will be passing ContestID And ContestOptions(Which is ContestOptionIds).
The ContestOptions will be somethin like this..
37|38|39
Since it is like that m passing it as Varchar,but after splitting those Ids i want it to be converted to bigint and then inserted.
Here is my SP,
ALTER PROCEDURE CreateCorrectOpt
@ContestId bigint,
@Options varchar(500)
AS
DECLARE @Totaloption int
SET @Totaloption = (SELECT COUNT(*) AS TotalOpt
FROM ContestCorrectOption
WHERE (ContestId = @ContestId))
IF @Totaloption < 1
BEGIN
DECLARE @OptionId bigint, @Pos int
SET @Options = LTRIM(RTRIM(@Options))+ '|'
SET @Pos = CHARINDEX('|', @Options, 1)
IF REPLACE(@Options, '|', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @OptionId = -1
print ':'+ LTRIM(RTRIM(LEFT(@Options, @Pos - 1))) + ':'
SET @OptionId = CONVERT(bigint, LTRIM(RTRIM(LEFT(@Options, @Pos - 1))))
IF @OptionId <> -1
BEGIN
INSERT INTO ContestCorrectOption
(ContestId, ContestOptionId)
VALUES (@ContestId,@Options)
END
SET @Options = RIGHT(@Options, LEN(@Options) - @Pos)
SET @Pos = CHARINDEX('|', @Options, 1)
END
END
END
Post #710298
Florian Reischl
Florian Reischl
Posted Tuesday, May 05, 2009 3:10 PM
SSCrazy
Group: General Forum Members
Last Login: Sunday, November 04, 2012 12:23 PM
Points: 2,087,
Visits: 3,932
Hi
If you just want to know how to convert VARCHAR to BIGIN please have a look to BOL for "CONVERT function"
Greets
Flo
The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL
How to Post Data/Code to get the best Help
How to Post Performance Problems
Post #710678
Giacomo Degli Esposti
Giacomo Degli Esposti
Posted Wednesday, May 06, 2009 4:43 AM
SSC Rookie
Group: General Forum Members
Last Login: Friday, May 03, 2013 1:38 AM
Points: 36,
Visits: 110
IF @OptionId <> -1
BEGIN
INSERT INTO ContestCorrectOption
(ContestId, ContestOptionId)
VALUES (@ContestId,@Options)
END
Maybe here you mistyped and wanted to insert @OptionID in stead of @Options ?
regards
Giacomo
Post #710919
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.