INSERT statement - SQL server- Error -Nvarchar value ‘2020’ overflowed TINYINT column - sql-server

Trying to insert value of Year i.e. 2020 into column with dataype TINYINT which is not possible because TINYINT stores from 0-255. Is there a workaround/solution (using Convert/Cast or any other possible way ) if I want to store the value 2020 without using a larger integer column (i.e. without changing the datatype from TINYINT to INT, BIGINT etc. )
I’m using SQL SERVER Management Studio.
Please help.

I understand that 2020 cannot be saved to tinyint and the datatype needs to be changed to int,bigint,varchar etc.
Further elaborating my question statement, I was required to enter Year somehow in TINYINT:
Without changing the datatype, and
By using inbuilt DATENAME and getdate() functions in SQL server.
So, I stored 20 as per above two requirements by using the below:
CONVERT(TINYINT,SUBSTRING(CAST(DATENAME(YEAR,GETDATE())AS CHAR),3,2))

alter yourTable alter column year varchar(4);
If you need as a number you need to use at least smallint (-32000 to 32000)
alter yourTable alter column year smallint;

Related

Converting varchar date field to datetime datatype [duplicate]

This question already has answers here:
Is there a way to convert a varchar to DATETIME in SQL SERVER 2008?
(4 answers)
Closed 2 years ago.
I made the mistake of using varchar to store my data that represents date/time. Now I'm trying to convert my column of text data, stored as a varchar, like so:
2020-11-25T14:22:41.3539327Z
Into a column that stores a datetime datatype.
Provided that all your data is in the format yyyy-MM-ddThh:mm:ss.nnnnnnn then you can just change the data type of the column:
ALTER TABLE dbo.YourTABLE ALTER COLUMN YourColumn datetime2(7);
If you need the timezone in there, then use datetimeoffset(7) instead.
-- to fix it you can use convert, below uses GetDate for examaple
SELECT convert(varchar, getdate(), 120)
depending on what precision you need you can use the link below to find it and change the 120 to whatever number you need for that precision.
https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
To fix your table you should follow these steps:
Add a new column to your table for DateTime
Run an update on your table using the Convert above to update the new column with the converted value from your varchar field.
Then drop the column with the varchar data.
Code to do the steps I outlined above
ALTER TABLE dbo.TableName ADD NewDateTimeCOL DATETIME
-- NOTE if your table is LARGE you will not want to do a direct update like this but do looping for performace purposes
UPDATE dbo.TableName
SET NewDateTimeCOL = convert(varchar, OldDateTimeCOL, 120)
ALTER TABLE dbo.TableName DROP COLUMN OldDateTimeCOL

How to insert Gujarati in SQL Server 2012 Management Studio?

How can insert Gujarati language in Microsoft SQL Server Management Studio?
I have tried to insert Gujarati and use nvarchar datatype but it's not working
The most common problem that I see when people aren't getting the outcome they want when inserting into an nvarchar column is that they aren't putting N in front of their string literal values (N is National Character Set).
If you have a table like this:
CREATE TABLE dbo.Test
(
TestID int IDENTITY(1,1) PRIMARY KEY,
TestValue nvarchar(100)
);
Here's a Chinese example that won't work (because of multi-byte string values):
INSERT dbo.Test (TestValue) VALUES ('Hello 你好');
With just single quotes, it's just an ASCII string. What would work is:
INSERT dbo.Test (TestValue) VALUES (N'Hello 你好');
I'm guessing your issue might be similar for Gujarati.
you can take column datatype Nvarchar and then you can insert in Gujarati

Alter Column: option to specify conversion function?

I have a column of type float that contains phone numbers - I'm aware that this is bad, so I want to convert the column from float to nvarchar(max), converting the data appropriately so as not to lose data.
The conversion can apparently be handled correctly using the STR function (suggested here), but I'm not sure how to go about changing the column type and performing the conversion without creating a temporary column. I don't want to use a temporary column because we are doing this automatically a bunch of times in future and don't want to encounter performance impact from page splits (suggested here)
In Postgres you can add a "USING" option to your ALTER COLUMN statement that specifies how to convert the existing data. I can't find anything like this for TSQL. Is there a way I can do this in place?
Postgres example:
...ALTER COLUMN <column> TYPE <type> USING <func>(<column>);
Rather than use a temporary column in your table, use a (temporary) column in a temporary table. In short:
Create temp table with PK of your table + column you want to change (in the correct data type, of course)
select data into temp table using your conversion method
Change data type in actual table
Update actual table from temp table values
If the table is large, I'd suggest doing this in batches. Of course, if the table isn't large, worrying about page splits is premature optimization since doing a complete rebuild of the table and its indexes after the conversion would be cheap. Another question is: why nvarchar(max)? The data is phone numbers. Last time I checked, phone numbers were fairly short (certainly less than the 2 Gb that nvarchar(max) can hold) and non-unicode. Do some domain modeling to figure out the appropriate data size and you'll thank me later. Lastly, why would you do this "automatically a bunch of times in future"? Why not have the correct data type and insert the right values?
In sqlSever:
CREATE TABLE dbo.Employee
(
EmployeeID INT IDENTITY (1,1) NOT NULL
,FirstName VARCHAR(50) NULL
,MiddleName VARCHAR(50) NULL
,LastName VARCHAR(50) NULL
,DateHired datetime NOT NULL
)
-- Change the datatype to support 100 characters and make NOT NULL
ALTER TABLE dbo.Employee
ALTER COLUMN FirstName VARCHAR(100) NOT NULL
-- Change datatype and allow NULLs for DateHired
ALTER TABLE dbo.Employee
ALTER COLUMN DateHired SMALLDATETIME NULL
-- Set SPARSE columns for Middle Name (sql server 2008 only)
ALTER TABLE dbo.Employee
ALTER COLUMN MiddleName VARCHAR(100) SPARSE NULL
http://sqlserverplanet.com/ddl/alter-table-alter-column

Multiple timestamp columns in SQL Server 2000

I need to create a table in SQL Server 2000.
create table TABLE (
DBID_ bigint not null,
CLASS_ varchar(255) not null,
DBVERSION_ integer not null,
HPROCI_ bigint,
TYPE_ varchar(255),
EXECUTION_ varchar(255),
ACTIVITY_NAME_ varchar(255),
START_ timestamp,
END_ timestamp,
DURATION_ bigint,
TRANSITION_ varchar(255),
NEXTIDX_ integer,
HTASK_ bigint,
primary key (DBID_)
);
An error occurs when I run it.
A table can only have one timestamp column. Because table TABLE
already has one, the column END_ cannot be added.
What is the best alternative for timestamp for SQL Server? How to fix this issue?
A timestamp is not a datetime datatype as the name suggests. It is an internal value that is relative to the server's clock, but an actual time cannot be derived from it's value. It is simply used to evaluate whether a row has been updated, and thus a table can only have one column of this type. The timestamp syntax is actually deprecated and is now named rowversion which makes a lot more sense.
Given your column names (Start, End) I assume you are trying to store actual timestamps, and should instead be using datetime as your datatype.
In Sql Server timestamp is a data type and it's not a time.
It's basically a way of versioning a record and it's used for optimistic locking in a disconnected database model
When you load up the record, you pick up the timestamp column. You only write it back if the value in the timestamp column is the same, as that means no one else has changed it since you got it.
If you want a real datetime value, add a datetime either not null, or with a default of GetDate() and remember to update every update.

Is it possible to alter a SQL Server table column datatype from bigint to varchar after it has been populated?

I have a SQL Server 2008 table which contains an external user reference currently stored as a bigint - the userid from the external table. I want to extend this to allow email address, open ID etc to be used as the external identifier. Is it possible to alter the column datatype from bigint to varchar without affecting any of the existing data?
Yes, that should be possible, no problem - as long as you make your VARCHAR field big enough to hold you BIGINT values :-)
You'd have to use something like this T-SQL:
ALTER TABLE dbo.YourTable
ALTER COLUMN YourColumnName VARCHAR(50) -- or whatever you want
and that should be it! Since all BIGINT values can be converted into a string, that command should work just fine and without any danger of losing data.

Resources