Adding default timestamp to a table in snowflake - snowflake-cloud-data-platform

I am trying to add a new column of timestamp type to a table with default values using following code;
ALTER TABLE "DATABASE"."SCHEMA"."TABLE" ADD COLUMN PRESENT_TIME TIMESTAMPNTZ DEFAULT CONVERT_TIMEZONE('UTC',current_timestamp())::TIMESTAMP_NTZ
But this is giving me an error;
SQL compilation error: Invalid column default expression [CAST(CONVERT_TIMEZONE('UTC', CAST(CURRENT_TIMESTAMP() AS TIMESTAMP_TZ(9))) AS TIMESTAMP_NTZ(9))]
Edit
ALTER TABLE "DATABASE"."SCHEMA"."TABLE" ADD COLUMN PRESENT_TIME TIMESTAMP
DEFAULT CURRENT_TIMESTAMP()
Error:
Invalid column default expression [CURRENT_TIMESTAMP()]
Can I kindly get help to rectify this error? thanks

CREATE OR REPLACE TABLE "DATABASE"."SCHEMA"."TABLE"
("ID" INT, "PRESENT_TIME" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP());

Giving a tip. Might be helpful seeing you struggle to convert time to utc. You can use sysdate() to get utc time.

Related

Add Snowflake calculated geography column

I've got a block of data with lat/longs and I'm trying to add a point to a Snowflake table from this data.
First I tried to accomplish it when I created the table with:
create or replace table geo (
best_lat double,
best_lon double,
geography geography as (ST_POINT(best_lon, best_lat)));
This errored out with SQL compilation error: error line 4 at position 2 Data type of virtual column does not match the data type of its expression for column 'GEOGRAPHY'. Expected data type 'GEOGRAPHY', found 'VARIANT'
Then I tried to add the column with:
alter table geo
add column geom geography as (select st_makepoint(best_lon, best_lat) from geo)
This errored out with SQL compilation error: Invalid virtual column expression [(SELECT ST_MAKEPOINT(GEO.BEST_LON, GEO.BEST_LAT) AS "ST_MAKEPOINT(BEST_LON, BEST_LAT)" FROM GEO AS GEO)]
Clearly I'm doing something wrong here. Can anyone provide some insight?
Snowflake doesn’t really support calculated columns, what it does do is allow you to set a default value for a column and this default can be a simple SQL statement. The syntax is documented here
Because this isn’t a pure calculated column, you can still insert values directly into the column, which will override the defined default value.

SQL Server 2014 - Weird datatype in a table

One of my tables has the data type of (date(datetime) when I expand it in object explorer but when I script out the create statement it shows date data type.
When I run a select script, I find that the column stores datetime data
So now my question is why the "CREATE TABLE" script is showing a wrong data type?
As Aaron noted in the comments, it's because someone created a custom datatype called DATE that pointed to the DATETIME datatype.
You can fix it (test first in development) by saying:
ALTER TABLE dbo.tablename ALTER COLUMN [LAST_SCAN_DATE] datetime --NOT NULL;

The conversion of a nvarchar data type to a datetime

I have execute new query as below:
alter table tenancy_extend_old alter column createdate datetime
but it show messages error as below:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Please help me. thanks
I suggest you:
-Create a new column called something like CD of type datetime
-Update the data accross using a suitable format. If your data is consistently in format Dec 18 2001 (which it probably isn't assuming the old column is varchar) then you can use this:
UPDATE tenancy_extend_old
SET CD = CONVERT(DATETIME,createdate ,100)
It's likely you'll get the same error because someone chose the incorrect data type originally, so you'll need to use Aarons suggestion to find bad data
Once you've done that, drop the old column and rename the new column

Is it possible to change the datatype of a column in a view?

Usually I run a script like this:
ALTER TABLE [TABLE]
ALTER COLUMN [Column] NVARCHAR(40);
The result is that the field in the table gets converted to nvarchar. But what is the syntax for doing the same thing for a view? Or is that even possible?
Sure
CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles
So check out CAST and also CONVERT on the msdn pages for full info
If you have updated the type in the table and the view still reflects the old type, simply running an alter view without changing anything will update the type
Yes..You can try Convert function to do this.
Convert (Desired datatype,column name)
eg.Convert(varchar(50),dbo.User_master.User_email)
where User_email has previous type as nvarchar(MAX).
If you want to convert nvarchar data to datetime then additional parameter is needed to Convert function like
CONVERT(data_type(length),expression,style)
eg. Convert(Datetime,dbo.User_master.User_DOB,103)
more info at SQL Server CONVERT() Function

Data migration from MySQL to HSQL

I was working on migrating data from MYSQL to HSQL.
In MYSQL data file, there are plenty of records where date values are set as '0000-00-00' and HSQL database throws below error:
"data exception: invalid datetime format / Error Code: -3407 / State:
22007"
for all such records.
I would like to know what could be optimum solution for this problem?
Thanks in advance
HSQLDB follows the SQL Standard and allows valid dates only. A date such as '0001-01-01' would be a good candidate for the default value.
Regardless of the method used for data inserts, the '0000-00-00' strings should be corrected before insert. One way of doing this is to use a default value for the target column with DEFAULT DATE'0001-01-01' and replace the string in the INSERT statement with the keyword DEFAULT. For example:
CREATE TABLE MYTABLE ( C1 INT, C2 DATE DEFAULT DATE'0001-01-01')
INSERT INTO MYTABLE VALUES 1, DEFAULT
INSERT INTO MYTABLE VALUES 3, '2010-08-14'

Resources