Computed Column Specification Based on Primary Key - sql-server-2008

I have a field that I want to compute based on a string and the ID generated when a record is inserted. Basically when a record is save with ID = 1, I need the computed field to read 'string_1' and so on. I am trying this is my formula (('PV'+'_')+ID) where PV is the string and ID is the primary key field in the same row as the data inserted but I'm getting a formula error. If I add quotes around ID then I just get PV_ID which is wrong. Any idea how I can reference the ID field in my formula and fetch the value?
here is my table row structure(ID,Computedfield,data1,data2). i need computedfield to have the value of the ID field concatenated with a string. any help appreciated
EDIT
Using SQL SERVER 2008 R2 Standard

Your question isn't totally clear on whether that prefix string is a string literal, or the contents of another column.
If it's a literal, you should be able to say:
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS 'PV_' + CAST(ID AS VARCHAR(10)) PERSISTED
If it's a string contained in another column, you should be able to define it like this
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS PV + '_' + CAST(ID AS VARCHAR(10)) PERSISTED
assuming PV is the column (of type VARCHAR) containing the prefix string.
The main point is: since you're mixing a literal string, and an INT value, you need to CAST the INT to a string first before being able to concatenate those two.

Use formula:
('PV_'+CAST(ID as varchar))
if you want to keep the resulting value - add the PERSISTED in the end

Related

How to update columns in a table for specific rows?

I want to update my columns for rows specified by WHERE command, but I want to update my field in a way that it extracts number part of the string from each specified field, multiplies that with a number (that I will specify) and give number output in all those specific fields extracted by WHERE command in that column.
For example, assume I want to update all my fields in a column which are like (5.6 AUD/1000, 4.5 AUD/1000, 9.7 AUD/1000), so I want to first identify fields ending with /1000 and update only those fields in the column by multiplying the number part of the string (which is 5.6, 4.5, 9.7) with any number (let's say 10). I want that other fields on the column remains unchanged.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
gives me all the specific fields that I wish to update. But I want to update them in the way I specified above, which is that I want to extract number part from the string and multiply that with a number and update those fields only.
I am able to extract all the fields with the condition I mentioned, I'm facing difficulty in update these fields in the column.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
SELECT CAST(Column8 AS UNSIGNED)*10 FROM sorted
wHERE
column8 REGEXP '/1000$';
The above code gives me required updated fields, but I want them reflected in my column.
I expect my output to be a column where only those fields ending with '/1000' should get updated in a way that the number part of the string is multiplied with 10.
I have casted the varchar field named string to decimal type and multiplied with static value 10 . I have checked in sql server.
DECLARE #temp TABLE
(
string NVARCHAR(50)
)
INSERT INTO #temp (string)
VALUES
('5.6 AUD/1000'),
('4.5 AUD/1000'),
('9.7 AUD/1000')
select cast(left(string, patindex('%[^0-9./]%', string) - 1) As decimal(18,2))* 10
from #temp

How to UPDATE a String with a int (+1) and convert back to String

I have the following problem.
An ID is stored in a field this field is declared as char(3).
I would like to rewrite this field and always count high by one from 001.
and save this value back to char(3). The leading zeros must be back on.
In addition, the value of the previous row must be determined.
It is a foreign software so I have to live with the realities.
Can someone help me here?
Old Value
char(3)
001
New Value
char(3)
001+1 = 002
UPDATE example SET id = id + 1 WHERE // Doenst work cause it is a string
You can use lpad():
UPDATE example
SET id = LPAD(id + 1, 3, '0')
WHERE . . .;
However, if your id is a number, you should probably store it as a number. In fact, I would recommend an auto-increment column, if that fits your needs.
Then you can use a view or generated column to return it as a string.

MySQL used ALL Type while searching on Primary Key

My table schema:
My above table has ~10L data. While using EXPLAIN, it shows as,
From this, type shows ALL, Extra shows Using where and rows not in O(1). But, for searching on primary key, the type should be const, rows be in O(1) ?? I can't able to figure out the issue, which results in slowing the queries.
Your id field is varchar while you pass the value you are looking for as a number.
This means that mysql has to perform an implicit data conversion and will not be able to use the index for looking up the value:
For comparisons of a string column with a number, MySQL cannot use an
index on the column to look up the value quickly. If str_col is an
indexed string column, the index cannot be used when performing the
lookup in the following statement:
SELECT * FROM tbl_name WHERE str_col=1;
The reason for this is that
there are many different strings that may convert to the value 1, such
as '1', ' 1', or '1a'.
Either convert your id field to number or pass the value as string.
As your id column is varchar, you need to provide it String while searching.
Try, id= '123456'
Reason:
Since you are comparing a varchar column to Int, it will first convert all rows to Int, and then match it with 123456(int).

how to set one field in a table equal to another field in a table in access

I have a table with two fields, IDCopy and ID. I want to copy the value of ID into IDCopy because ID is a number field and I need a second copy of this field as a text field.
I am used to doing things like this on sql server
UPDATE table SET table.IDCopy= table.ID;
But when I try to run that query in access it asks me for the parameter value of ID. What is the syntax for setting one column in a table to another column in Access?
You can use CStr() to cast the ID number to text. This should work when IDCopy is text and ID is numeric.
UPDATE [table] SET IDCopy = CStr(ID);
I bracketed the table name because table is a reserved word.
If Access still thinks ID is a parameter with this query, then [table] does not include a field named ID.

How to achieve similar datatype

Goal:
Combine two column named first and lastname, from the same table A and then transfer it to column fullname in table B from a another
relational database.
Column first and lastname has the same datatype as fullname. The datatype is varchar(50) or varchar(100).
Problem:
I can't make the transaction to have the same datatype
You need to use the type cast expression DT_STR in Derived Column transformation so that the output from Derived Column transformation is still in varchar data type.
Below shown Derived Column Transformation shows two new columns.
First new column FullName takes in two input columns FirstName and LastName. Concatenates the columns with a space to separate them and then type casts to DT_STR. In (DT_STR, 100, 1252), 100 represents the length of the output column, 1252 represents the code page.
Second new column FullNameNoCast simply concatentates the two input columns FirstName and LastName. This will result in Unicode data type.
Since, you mentioned that your destination is of varchar data type. I believe that you are not type casting the new column in Derived Column transformation. That might lead to the error you are facing.
Hope that helps.