SQL Datatype bit query - sql

Trying to find a sql query for data type bit for the following:
update the an columen to '1' where the date is <= the current date.
Can anyone help

Update MyTable
Set ColumnOfTypeBit = 1
Where ColumnOfTypeDate <= CURRENT_TIMESTAMP;

Related

Set Variable Varchar2 to date fields

I have a Script that I update a Date field
this is a part of my script
var VARCHAR2(8 char);
var = '01/01/22';
update table1 t set t.date = to_date(var,'dd/mm/yy');
and then I got a null value in field date
why I dont have a 01/01/22 value ??
could you help me please !!
ANSWER ::::::::::: PETIT PAPA merci :D
I created I new Varible with Date type
and I affecte the to_date to this variable
date_var Date;
date_var :=to_date(var,'dd/mm/yy');
update table1 t set t.date = date_var where ...;
and it work THANK YOU everyONE
Try
to_date(var,'dd/mm/yy');
try this:
var := '01/01/22';
update table1 t set t.date = to_date(var,'dd/mm/yy');
commit;
I try this code to me, and work fine:
create table t( data date);
Then
insert into t (data)
values (to_date('01/01/22','dd/mm/yy'));
Now we check data in table
select * from t;
We got expected result. That's for inserting.
For updating
update t set data = to_date('01/02/22','dd/mm/yy');
Now check
select * from t;
We got 01-FEB-22
But beware when updating without where clause it will update all records
Miistake here is that it's not todate it is to_date

Convert INT column to Datetime in SQL Server

I have a column as int and it stores year like 2017.
How can I convert this to save the result to another actual datetime column like 2017-01-01?
DATEFROMPARTS is a new SQL Server function, (from SQL Server 2012), that allows to build a date value using its parts: Year, Month, Day.
Have a look at DATEFROMPARTS at MS Docs.
UPDATE TableName
SET <UpdColName> = DATEFROMPARTS(<IntColName>,1,1)
WHERE <some condition>
Use the function DATEFROMPARTS.
select datefromparts(colname,1,1)
from tablename
Try using DATEFROMPARTS,
UPDATE table SET coldatetime= datefromparts(colname,1,1) WHERE colname = 2017
This is complicated. First, convert the column to a varchar():
alter table t alter column col varchar(255);
When you do this, the integer will be converted to a varchar().
Next, append '0101' to the value:
update t
set col = col + '0101';
This puts the value in the form 'YYYYMMDD', which SQL Server recognizes as a date.
Finally, alter to a date:
alter table t alter column col date;
If you like, you can add another column to the table for the date and do this in two steps:
alter table t add column datecol date;
update t
set datecol = concat(intcol, '0101');
Although you can also use datefromparts(), the above should work in earlier versions of SQL Server as well.

SQL - update now() data with only date

i have an SQL database with a field which is filled by now() function.
i want to update the present data only with date part of it.
Example :
Present Data :
20.08.2015 13:10:31
21.08.2015 14:00:29
22.08.2015 05:55:42
Target
20.08.2015
21.08.2015
22.08.2015
thanks,
Assuming you are using the MySQL database, you can use date function
update yourtable
set yourColumnName = date(yourColumnName)
In SQL Server try using the Convert function
update yourtable
set yourColumnName =CONVERT(date, yourColumnName)
or
update yourtable
set yourColumnName CONVERT(varchar(10),yourColumnName,104)
or else you can use the LEFT function like
update yourtable
set yourColumnName =LEFT (yourColumnName, 10)
SQL FIDDLE
Assuming the data is in a table, you would do something like:
update t
set col = cast(col as date);
The exact syntax for converting to a date varies by database. It might also be:
date(col)
date_trunc('day', col)
trunc(col)

How to Update Column,whose datatype is DateTime

I have table named as dbo.SetCustomerRFMComData with eight columns.
In which the third column name is RecDatetime whose DataType is DATETIME
I update the table using an stored procedure.
How to add current Datetime in RecDateTime column while firing an UPDATE QUERY
In SQL Server use GETDATE() .
In MySQL and PostgreSQL , use NOW().
In Oracle , use SYSDATE.
Call GETDATE() method which will update you current date time
Suppose your sp looks like this
create procedure sp_proc
(
#RecTime Datetime
)
as
begin
set #RecTime =DateTime()
update tablename values(RecTime=#RecTime )
end

error while adding a column in oracle 10 g

i am trying to add a column validFrom to my table
this is my query
alter table mytable add validFrom date default getdate() not null
i am getting error like
ORA-04044: procedure, function, package, or type is not allowed here
please help
Oracle does not have a getdate() function - something which can easily be found out by looking into the manual.
You need to use SYSDATE or CURRENT_DATE
ALTER TABLE mytable ADD (validFrom date DEFAULT sysdate);