 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 31st, 2006, 01:24 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
error in query
i am trying to run a stored procedure and there is a problem in this particular code
DECLARE @MidVchar char(10), @DOBdate smalldatetime, @PreId int
Select @MidVchar = datename(yy, getdate()) + 'FRE'
Select @PreId = max(MemberCodeint) from LoginRegFree
Select @MidVchar = @MidVchar & @PreId
and the error message is
Server: Msg 245, Level 16, State 1, Line 6
Syntax error converting the varchar value '2006FRE ' to a column of data type int.
Here MemberCodeint is type int (as identity)
and @MidVchar is Varchar
thanks in advance
ashok sharma
|
|

March 31st, 2006, 01:49 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
First the "&" is a bitwise operator. If you are trying to concatenate strings you use "+"
Second you are trying to concatenate a char var (@MidVhar) with an integer (@PreID)
you need to convert the interger var
Something like
Select @MidVchar = @MidVchar + convert(varchar(10),@PreId)
Jim
|
|

March 31st, 2006, 03:26 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
i have already tried this, the query is running but result is unexpedted
as per query the result must be 2006FRE1 ----- PreId = 1
but the result is 2006FRE ----- PreId is not concanated
and there is no error
ashok
|
|

March 31st, 2006, 10:56 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
what do you mean there is no error, your post says there is an error..
What are you trying to do?
|
|

April 3rd, 2006, 01:20 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
Hi,
i mean to say the changes you told to make, i changed. and after that there is no error but the result is unexpected.
i changed my code to
DECLARE @MidVchar char(10), @DOBdate smalldatetime, @PreId int
Select @MidVchar = datename(yy, getdate()) + 'FRE'
Select @PreId = max(MemberCodeint) from LoginRegFree
Select @MidVchar = @MidVchar + convert(varchar(10),@PreId)
as per query the result must be 2006FRE1 ----- PreId = 1
but the result is 2006FRE ----- PreId is not concanated
ashok
|
|

April 3rd, 2006, 01:30 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
You need to change @MidVchar from char(10) to varchar(10) or varchar(what-ever-number-you-need).
Gert
|
|

April 3rd, 2006, 03:23 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
thanks gert
Probem Solved
can you tell me now how can i add preceeding zeros. for example when i run my sp the result is 2006FRE1 BUT i want it like
2006FRE00001
so how to add preceeding zeros in my @PreId (the length of this field is 5 character means till 99999)
thanks
ashok
|
|

April 3rd, 2006, 08:03 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi again.
Maybe something like this?
Select @MidVchar = @MidVchar + RIGHT('00000'+convert(varchar(10),@PreId),5)
Gert
|
|

April 3rd, 2006, 11:32 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Nice one Gert!
|
|

April 4th, 2006, 02:16 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
hi Gert
great solution
thanks
ashok
|
|
 |