Set datetime to null Bigquery - google-bigquery

How does one set a column with datatype datetime to null?
I searched through the docs and I couldn't find something towards that.
Any suggestions?

Check if this column is NULLABLE.
If it is - just use UPDATE statement:
UPDATE mytable
SET mycolumn = NULL
WHERE myconditions
If it is REQUIRED - you can change REQUIRED to NULLABLE - link

Related

Not able to update column which was set as null using 'select into' statement in sybase

I am creating a temp table in sybase like below
select col1 = null, col2 =2 into #myTable
Here when I try to update col1
update #myTable set col1 = 'test'
I get error - "[Error Code: 257, SQL State: 42000] Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query."
Can anyone please help me fix it?
Assuming this is Sybase ASE (257 is a standard ASE system error number) ...
col1=null doesn't tell the database what the datatype of col1 should be so the database defaults the column's datatype to int.
When creating a table via select/into you need to insure each column is created with the desired datatype. For this particular instance try:
select col1=convert(varchar(10),null), col2=convert(tinyint,2) into #myTable
NOTES:
modify the convert() calls to reference the desired datatypes
when the new column is populated from another table's column(s) the source column(s) datatypes will be used in determing the datatype of the new column
Also keep in mind the following:
select col1='test' into #otherTable
The datatype for col1 will be determined from the initial data value; in this case the value 'test' tells the database you need to store 4 characters so the database will default the column's datatype to varchar(4). This should be fine as long as you never intend to insert anything longer than varchar(4) otherwise you'll need to provide a convert() with the initial select/into to explicitly state the column's datatype, eg:
select col1=convert(varchar(35),'test') into #otherTable
Assuming you get past the Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. error message your next bump-in-the road may occur if you try something like:
update #myTable set col2 = NULL
With the result being that you're presented with an error message similar to column does not allow nulls.
As with datatype determination, Sybase (ASE) will try to determine a column's NULLability in a few different ways:
if column is assigned a 'value' of NULL (as in the example: col1 = null) then ASE will configure the column to allow NULLs
if the column's value is being copied from another table then the source column's NULLability will be used in determining the new column's NULLability
if the query explicitly defines the column as NULLable (see example - below) then the column will be configured to allow NULLs
if the database option allow nulls by default is false (ASE default setting) then the column's NULLability will be set to 'not NULL'
when all else fails ...
if the database option allow nulls by default is true then the column's NULLability will be set to 'NULL'(able)
An example of explicitly defining the column to allow NULLs:
select col1 = convert(varchar(35) null,'test') into #otherTable

SQL statement to change the value of a column with that same column in the where clause

I have a SQL Server database and I've changed my mind and instead of a column in my table being set as an int, I want to change it to a varchar. So I changed the type to varchar(8), and saved my changes in SQL Server Management Studio tool (V17.0).
It looks like the tool converted the int values to varchars when I saved the changes. I want to change the value of '1' to 'External', '2' to 'SPTR' and '3' to 'Other'. I was going to do one value at a time.
This is the simple SQL statement I tried:
UPDATE mytable
SET mycolumn = 'External'
WHERE mycolumn = '1'
The error message I get from SSMS is
Conversion failed when converting the varchar value 'External' to data type int"
It's as if the database thinks the type is int, but it's not, it's varchar(8).
Sounds like you really haven't changed the data type. This should resolve the problem.
ALTER TABLE mytable ALTER COLUMN mycolumn varchar(8);
GO
UPDATE mytable
SET mycolumn = CASE mycolumn WHEN '1' THEN 'External'
WHEN '2' THEN 'SPTR'
ELSE 'OTHER'
END;
Note, as well, you can update every value at the same time by using a CASE expression. Likely far easier than 3 UPDATE statements.
ALTER TABLE mytable ALTER COLUMN mycolumn varchar(50);
SELECT DATALENGTH ('External')
returns 8
As long as I know varchar does not use all 50 bytes, it only uses 8 in your case to store "External" field, why don't you try to change table size.
If you have changed mycolumn to a string, then the following should work:
UPDATE mytable
SET mycolumn = 'External'
WHERE mycolumn = '1';
If you are getting a type conversion error, then I assume you have a trigger on the table that is causing the problem. You might be wrong in saying that the type has changed. But even if you are correct, a trigger could still generate this issue.

Set a field to the value of another field [duplicate]

Is it possible to copy data from column A to column B for all records in a table in SQL?
How about this
UPDATE table SET columnB = columnA;
This will update every row.
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
This will update all the rows in that columns if safe mode is not enabled.
UPDATE table SET columnB = columnA;
If safe mode is enabled then you will need to use a where clause.
I use primary key as greater than 0 basically all will be updated
UPDATE table SET columnB = columnA where table.column>0;
If you want to copy a column to another column with a different data type in PostgresSQL, you must cast/convert to the data type first, otherwise it will return
Query 1 ERROR: ERROR: column "test_date" is of type timestamp without
time zone but expression is of type character varying LINE 1: update
table_name set test_date = date_string_col
^ HINT: You will need to rewrite or cast the expression.
An example of converting varchar to timestamp:
update table_name set timestamp_col = date_string_col::TIMESTAMP;
An example of converting varchar to int:
update table_name set int_column = string_col::INTEGER;
but any column type(except file or the similar) can be copied to string(character varying) without cast the type.

NULL value for int in Update statement

Is it possible to set NULL value for int column in update statement?
How can I write the update statement in this case?
Assuming the column is set to support NULL as a value:
UPDATE YOUR_TABLE
SET column = NULL
Be aware of the database NULL handling - by default in SQL Server, NULL is an INT. So if the column is a different data type you need to CAST/CONVERT NULL to the proper data type:
UPDATE YOUR_TABLE
SET column = CAST(NULL AS DATETIME)
...assuming column is a DATETIME data type in the example above.
By using NULL without any quotes.
UPDATE `tablename` SET `fieldName` = NULL;
Provided that your int column is nullable, you may write:
UPDATE dbo.TableName
SET TableName.IntColumn = NULL
WHERE <condition>
If this is nullable int field then yes.
update TableName
set FiledName = null
where Id = SomeId

How do I set a column value to NULL in SQL Server Management Studio?

How do I clear the value from a cell and make it NULL?
I think Zack properly answered the question but just to cover all the bases:
Update myTable set MyColumn = NULL
This would set the entire column to null as the Question Title asks.
To set a specific row on a specific column to null use:
Update myTable set MyColumn = NULL where Field = Condition.
This would set a specific cell to null as the inner question asks.
If you've opened a table and you want to clear an existing value to NULL, click on the value, and press Ctrl+0.
If you are using the table interface you can type in NULL (all caps)
otherwise you can run an update statement where you could:
Update table set ColumnName = NULL where [Filter for record here]
Use This:
Update Table Set Column = CAST(NULL As Column Type) where Condition
Like This:
Update News Set Title = CAST(NULL As nvarchar(100)) Where ID = 50
Ctrl+0 or empty the value and hit enter.
Just as a little extension to Jeff Martin's and Zack Peterson's solution.
If you still want to set several values from the columns to null you can simply set the query to
UPDATE myTable SET MyColumn1 = NULL, MyColumn2 = NULL, MyColumn3 = NULL, ...
CTRL+0 doesn't seem to work when connected to an Azure DB.
However, to create an empty string, you can always just hit 'anykey then delete' inside a cell.