dbTalk Databases Forums  

Problem creating string in Computed Column Specification inManagement Studio

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Problem creating string in Computed Column Specification inManagement Studio in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jobs@thomassharp.com
 
Posts: n/a

Default Problem creating string in Computed Column Specification inManagement Studio - 07-25-2010 , 10:48 AM






Hi

I'm fairly novice to SQL Server and am trying to make a computed
column in my database

I have the following columns in tblJobs :

JobID
Title
City
State

I'm trying to create a computed column that takes the Title and City
and strips out any spaces and replaces them with a dash, then
concatenates the other columns with dashes. I also have a numeric
ID. So if I have

JobID : 22333
Title : Nurse Assistant
City : Panama City
State : FL

and want the resulting string to look like this : Nurse-Assistant-
Panama-City-FL-22333

I put the following string into the Formula of the Computed Column
Specification:

Replace(tblJobs.Title," ", "-") + "-" + Replace(tblJobs.City," ", "-")
+ "-" + State + "-" + (nvarchar(5), JobID)

yet the Management Studio says it can't validate this.

Am I on the right track here? Or should I try to do the replace (or
typecast) somewhere else?

Thanks for any help you can provide.

Paul

Reply With Quote
  #2  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: Problem creating string in Computed Column Specification inManagement Studio - 07-25-2010 , 11:19 AM






You are on the right track. Where it probably goes wrong is at

"+ (nvarchar(5), JobID)"

You forgot the word CONVERT, as in

"+ CONVERT(nvarchar(5), JobID)"

--
Gert-Jan


jobs (AT) thomassharp (DOT) com wrote:
Quote:
Hi

I'm fairly novice to SQL Server and am trying to make a computed
column in my database

I have the following columns in tblJobs :

JobID
Title
City
State

I'm trying to create a computed column that takes the Title and City
and strips out any spaces and replaces them with a dash, then
concatenates the other columns with dashes. I also have a numeric
ID. So if I have

JobID : 22333
Title : Nurse Assistant
City : Panama City
State : FL

and want the resulting string to look like this : Nurse-Assistant-
Panama-City-FL-22333

I put the following string into the Formula of the Computed Column
Specification:

Replace(tblJobs.Title," ", "-") + "-" + Replace(tblJobs.City," ", "-")
+ "-" + State + "-" + (nvarchar(5), JobID)

yet the Management Studio says it can't validate this.

Am I on the right track here? Or should I try to do the replace (or
typecast) somewhere else?

Thanks for any help you can provide.

Paul

Reply With Quote
  #3  
Old   
ptvvee
 
Posts: n/a

Default Re: Problem creating string in Computed Column Specification inManagement Studio - 07-25-2010 , 12:15 PM



Thank you so much Gert-Jan. That worked!! I had to actually replaces
the double quotes with single quotes and get rid of the table
declaration for it to work. So, here's what it ended up being :

((((((replace([Title],' ','-')+'-')+replace([City],' ','-'))+'-')+
[State])+'-')+CONVERT([nvarchar](5),[JobID],0))

Thanks again!!! Paul

On Jul 25, 12:19*pm, Gert-Jan Strik
<sorrytoomuchspamalre... (AT) xs4all (DOT) nl> wrote:
Quote:
You are on the right track. Where it probably goes wrong is at

*"+ *(nvarchar(5), JobID)"

You forgot the word CONVERT, as in

*"+ *CONVERT(nvarchar(5), JobID)"

--
Gert-Jan

j... (AT) thomassharp (DOT) com wrote:

Hi

I'm fairly novice to SQL Server and am trying to make a computed
column in my database

I have the following columns in tblJobs :

JobID
Title
City
State

I'm trying to create a computed column that takes the Title and City
and strips out any spaces and replaces them with a dash, then
concatenates the other columns with dashes. *I also have a numeric
ID. *So if I have

JobID : 22333
Title : Nurse Assistant
City : Panama City
State : FL

and want the resulting string to look like this : Nurse-Assistant-
Panama-City-FL-22333

I put the following string into the Formula of the Computed Column
Specification:

Replace(tblJobs.Title," ", "-") + "-" + Replace(tblJobs.City," ", "-")
+ "-" + State + "-" + *(nvarchar(5), JobID)

yet the Management Studio says it can't validate this.

Am I on the right track here? *Or should I try to do the replace (or
typecast) somewhere else?

Thanks for any help you can provide.

Paul

Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Problem creating string in Computed Column Specification in Management Studio - 07-25-2010 , 12:18 PM



(jobs (AT) thomassharp (DOT) com) writes:
Quote:
I put the following string into the Formula of the Computed Column
Specification:

Replace(tblJobs.Title," ", "-") + "-" + Replace(tblJobs.City," ", "-")
+ "-" + State + "-" + (nvarchar(5), JobID)
In addition to Gert-Jan's comment, observe that the string delimiter in
SQL is ' not ".

Also, you need need to include the table name as a prefix; I don't even
think this is permitted.

Quote:
yet the Management Studio says it can't validate this.
The table designer in Management Studio is a crappy tool, and I would not
be surprised if is something it does not support. You are better off using
CREATE TABLE.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.