From NUMERIC to NVARCHAR in Netezza - netezza

How convert a value NUMERIC(18,0) to NVARCHAR(10) in Netezza?
I have the column GL2RDAP with a NUMERIC(18,0) value and I need to transform in NVARCHAR(10)

to_char() function should do the trick
This is a link to the manual pages: https://www.ibm.com/support/knowledgecenter/en/SSULQD_7.2.1/com.ibm.nz.dbu.doc/r_dbuser_ntz_sql_extns_conversion_funcs.html

Related

INSERT statement - SQL server- Error -Nvarchar value ā€˜2020ā€™ overflowed TINYINT column

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;

Inserting a default GUID value into table columns

I'm trying to seed a table, and for the initial data load I would like the CreateBy column to have the value of the default GUID 00000000-0000-0000-0000-000000000000. Is there a simple way to do this? Is there a way to get NEWID() to generate the default value?
ALTER TABLE dbo.YOUR_TABLE ADD CONSTRAINT
DF_UID DEFAULT CAST ('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER) FOR COLUMN_NAME
Just add a default to the column
You could just explicitly cast this string:
INSERT INTO mytable
(createdBy)
VALUES (CAST ('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER));
I think the simplest approach is to cast a binary zero to a guid:
SELECT CAST(0x0 AS UNIQUEIDENTIFIER);
This will be casted implicitly actually. Try this:
Setting a typed GUID-variable to 0x0
DECLARE #id1 UNIQUEIDENTIFIER;
SET #id1=0x0;
SELECT #id1;
Assign 0x0 directly
DECLARE #id2 UNIQUEIDENTIFIER=0x0;
SELECT #id2;
Or use this against a table's column:
DECLARE #tbl TABLE(Id UNIQUEIDENTIFIER)
INSERT INTO #tbl(id) VALUES(0x0);
SELECT * FROM #tbl;
And it will work as default constraint too:
DECLARE #tbl TABLE(Id UNIQUEIDENTIFIER DEFAULT (0x0), SomeOtherValue VARCHAR(100));
INSERT INTO #tbl(SomeOtherValue) VALUES('GUID by default');
SELECT * FROM #tbl;
Hint: This looks as if you have a user's table with GUIDs as IDs and this is the FK-id pointing to this table. In this case you will have to maintain a user with a zero-id as dummy. To be honest: I would rather recommend to have a nullable column and mark the missing value with a NULL instead with a magic value...

How to declare a field as varchar(max) in Firebird

How can I declare this table with varchar(max) in Firebird?
CREATE TABLE MyUser
(
Id INT, -- unique id of user
Signature VARCHAR(max),
Login VARCHAR(50),
UserPassword VARCHAR(100),
CONSTRAINT PK_MyUser PRIMARY KEY (Id)
);
COMMIT;
Is it possible?
Firebird doesn't have a type VARCHAR(MAX). You either need to use VARCHAR(32765) assuming you are using a 1-byte character-set or VARCHAR(8191) (with UTF-8), or you need to use a BLOB SUB_TYPE TEXT (which is a blob type for text data).
As far as I know, it isn't.
Use VARCHAR(32765), Firebird can hold up to 32,765 characters (max). Or you could also use BLOB.

Change column from VARCHAR(MAX) to VARBINARY(MAX)

I have a table which has a VARCHAR(MAX) column, and I need to change it to VARBINARY(MAX).
I tried using the command
ALTER TABLE TableName ALTER COLUMN ColumnName VARBINARY(MAX)
but I got the error
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type varchar(max) to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
The table has no data, so I can't understand why it's complaining about data conversion.
You cannot perform this conversion using an ALTER TABLE statement since converting from varchar(max) to varbinary(max) requires an explicit conversion. So you should follow these steps to alter your table:
Alter table with new column of VARBINARY(MAX)
If you have existing data in the VARCHAR(MAX) column, use update statement to add the data to the VARBINARY column
Alter table to drop the VARCHAR(MAX) column
Rename varbinary column to varchar name (per comment from #Ben Thul)
Convert Varchar to Int and then change Int to Binary.

Alter column from NVARCHAR to NBINARY

I accidentally created a column with the wrong type NVARCHAR (for storing password salts) and I want to convert it to NVARBINARY.
I tried
ALTER TABLE [dbo].[TableName]
ALTER COLUMN [ColumnName] [varbinary] (20) NOT NULL
GO
but it says
Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.
Is there a way to do that? CONVERT seems to be for expressions only, not for alterations.
The only way by altering will be someting like:
Create Table a (id int,blb Nvarchar(10))
insert into a Values
(1,'Test'),
(2,N'Test2');
BEGIN Transaction
ALTER TABLE a
ADD blb_New [varbinary] (20) NULL
GO
UPDATE a
SET blb_new = CAST(blb AS varbinary(20))
GO
ALTER TABLE a
DROP COLUMN blb
GO
EXEC sp_rename 'a.blb_new', 'blb', 'COLUMN'
GO
COMMIT Transaction
Select *,CAST(blb as Nvarchar(20)) from a
Drop Table a
You may first convert all the values from NVARCHAR to NVARBINARY in the same column.
After converting all the values use Alter table
You may refer the following link:
Converting NVARCHAR(255) to DATE

Resources