I want to fetch autoincremented varchar values from SQL Server 2008 into a textbox in asp.net in pageload event.
Suppose I have a column trn_no, data type varchar. If the 1st value inserted manually in the table, like T100, then how would I get the value T101 in the textbox in pageload.
please help me.
If you want to have alphanumeric values in key then use following sql to get last inserted id +1.
SELECT 'T'
+ Cast(Max(Substring(trn_no, 2, Len(trn_no)-1) + 1 ) AS varchar(15))
FROM table
Change Varchar(15) as per possible max length
Related
I have a table in SQL Server with a column called "CreatedDate" (type: DateTime). I want to update all records by adding 3 hours to existing value. All rows are created on a different DateTime.
If a record was created at 07:33 then after running the query it should have value 10:33.
Use DATEADD:
UPDATE yourTable
SET CreatedDate = DATEADD(hour, 3, CreatedDate);
I have a column in my table that is of float type. The table was automatically generated when I imported the spreadsheet (Excel) data to my database. Thus there is a column I wish to change from float to varchar, but when I try to do this, I get an error:
'tblInvoices' table
Unable to create index 'IX_tblInvoices'.
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.tblInvoices' and the index name 'IX_tblInvoices'.
The duplicate key value is (1.00001e+006). The statement has been terminated.
It is a unique column, and set that way (not set as the primary key for reasons). I have already run queries to search for and delete duplicate fields but there are none. The query I ran as follows:
WITH CTE AS
(
SELECT
Invoice,
RN = ROW_NUMBER()OVER(PARTITION BY Invoice ORDER BY Invoice)
FROM
dbo.tblInvoices
)
DELETE FROM CTE
WHERE RN > 1
So the value within the Invoice column is 1000010 and when I run the following query a single row is found.
SELECT *
FROM [TradeReceivables_APR_IFRS9].[dbo].[tblInvoices]
WHERE Invoice = 1.00001e+006
Note that I have searched for the value in the error, 1.00001e+006, and not 1000010.
So my question is why does the DBMS do this? Why does it change the value like that? When I remove the column, it does it with another column and so on and so on (about 40 000 rows in total). How can I change the column from float to varchar without changing the data and getting errors?
Any help will be greatly appreciated!
It seems that the field is an integer so you can Cast it to BIGINT before cast to VARCHAR
Declare #Invoice as float = 1.00001e+006
print cast(#Invoice as varchar) -->> Result : 1.00001e+006
print cast(cast(#Invoice as bigint) as varchar) -->> Result : 1000010
I'm using SQL Server 2008 R2. I want to create an auto-generated customer id whenever a new customer is registered. The customer id should be in the following format
Customer ID =
current year (4 digit) +
current month (2 digit) +
unique number (4 digit).
Example: 2012055001
I have written a query as follows:
create table tb
(
id int identity(1000, 1),
cust_id as CONVERT(VARCHAR(4), DATEPART(YYYY, GETDATE())) +
SUBSTRING(CONVERT(nvarchar(6), GETDATE(), 112), 5, 2) +
CONVERT(VARCHAR(4), id) persisted primary key
);
But I'm getting the following error:
Computed column 'cust_id' in table 'tb' cannot be persisted because the column is non-deterministic.
How do I rectify this problem?
You're confusing structure with data. You're trying to create your table with data as the structure type, and that clearly can't work.
The column name would be cust_id, but the column type has to be one of the valid SQL Server data types (in this case most likely CHAR(10), as it's a fixed width and there are no Unicode characters possible).
CREATE TABLE tb(id int identity(1000, 1), cust_id Char(10));
Your auto-generated data would then be inserted into the column, most likely by an ON INSERT trigger. (How to create that trigger is a totally different topic; you can find information in most SQL tutorials or books, or in SQL Server documentation.)
I have the following table:
tbl_ProductCatg
Id IDENTITY
Code
Description
a few more.
Id field is auto-incremented and I have to insert this field value in Code field.
i.e. if Id generated is 1 then in Code field the value should be inserted like 0001(formatted for having length of four),if id is 77 Code should be 0077.
For this, I made the query like:
insert into tbl_ProductCatg(Code,Description)
values(RIGHT('000'+ltrim(Str(SCOPE_IDENTITY()+1,4)),4),'testing')
This query runs well in sql server query analyzer but if I write this in C# then it insets Null in Code even Id field is updated well.
Thanks
You may want to look at Computed Columns (Definition)
From what is sounds like you are trying to do, this would work well for you.
CREATE TABLE tbl_ProductCatg
(
ID INT IDENTITY(1, 1)
, Code AS RIGHT('000' + CAST(ID AS VARCHAR(4)), 4)
, Description NVARCHAR(128)
)
or
ALTER TABLE tbl_ProductCatg
ADD Code AS RIGHT('000' + CAST(id AS VARCHAR(4)), 4)
You can also make the column be PERSISTED so it is not calculated every time it is referenced.
Marking a column as PERSISTED Specifies that the Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated.
Unfortunately SCOPE_IDENTITY isn't designed to be used during an insert so the value will not be populated until after the insert happens.
The three solutions I can see of doing this would be either making a stored procedure to generate the scope identity and then do an update of the field.
insert into tbl_ProductCatg(Description) values(NULL,'testing')
update tbl_ProductCatg SET code=RIGHT('000'+ltrim(Str(SCOPE_IDENTITY()+1,4)),4) WHERE id=SCOPE_IDENTITY()
The second option, is taking this a step further and making this into a trigger which runs on UPDATE and INSERT. I've always been taught to avoid triggers where possible and instead do things at the SP level, but triggers are justified in some cases.
The third option is computed fields, as described by #Adam Wenger
I'd like to be able to select a row of data but have the first column of output be column name and the second column of output be the value of that column for the selected row. How might I do this in SQL Server 2005 SSMS?
Use the PIVOT operator.