Set Variable Varchar2 to date fields - sql

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

Related

SQL: Invalid column name after table updated

If I run the code below it gives invalid column name. It also gives problems later down in my code.
ALTER TABLE #final
ADD DOB2 DATE
UPDATE #final
SET DOB = '1980-01-01' where DOB ='NULL'
UPDATE #final
SET DOB2 = CAST(DOB as date)
Any reason why? I've already tried CTRL + SHIFT + R
Thank you!
T-SQL (which I presume you are using) compiles lines in batches. A batch is determined by GO boundaries. The problem is the code is compiled before the first statement is executed, so DOB2 is not known during the compile phase -- it hasn't been added yet.
You can easily fix this by adding GO:
ALTER TABLE #final ADD DOB2 DATE
GO
UPDATE #final
SET DOB = '1980-01-01'
WHERE DOB = 'NULL'
UPDATE #final
SET DOB2 = CAST(DOB as date);
I'm not sure what DOB = 'NULL' is supposed to be doing. Dates should be stored using the DATE data type. The DATE data type does not store string values, such as 'NULL'. Perhaps you intend WHERE DOB IS NULL.

Scalar Variable error using var Table but not Temp Table

I am stumped with this one. I have the following code it works fine up to the point of the last #POC_XLATE in the update statement and then I get the error MUST DECLARE SCALAR VARIABLE.
If I change the table to a temp table the code works fine. I have tried moving the select statement to the end of the code, that didn't work. Hope someone has some suggestion on why it is doing this. Thanks in advance.
declare #POC_XLATE as TABLE(
POC_XLATE_ID int NULL,
TAR_ID int NULL,
POC_USERID varchar(50) NULL,
ACTION_DATE datetime NULL
)
insert into #POC_XLATE(POC_XLATE_ID, TAR_ID, POC_USERID, ACTION_DATE)
select * from POC_XLATE
where POC_XLATE.ACTION_DATE is null
select * from #POC_XLATE
update #POC_XLATE
set ACTION_DATE = TAR_DATA.OPEN_DATE
from TAR_DATA
where #POC_XLATE.TAR_ID = TAR_DATA.TAR_ID
A column alias cannot start with a #. That is the sign for a declared scalar variable. So, use table aliases:
update p
set ACTION_DATE = td.OPEN_DATE
from #POC_XLATE p JOIN
TAR_DATA td
on p.TAR_ID = td.TAR_ID ;
But why you would write the query in two steps?
insert into #POC_XLATE(POC_XLATE_ID, TAR_ID, POC_USERID, ACTION_DATE)
select p.POC_XLATE_ID, p.TAR_ID, p.POC_USERID, td.OPEN_DATE
from POC_XLATE p left join
TAR_DATA td
on p.TAR_ID = td.TAR_ID
where p.ACTION_DATE is null;
One step is much cleaner than two.

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)

SQL Datatype bit query

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;

SQL script replacing column's data

I have a PostgreSQL database and I want to make a script that replaces the data of one column of the table A with the data of an other tables column. I've written this PL/PgSQL function:
BEGIN;
CREATE TEMPORARY TABLE tmp_table (id bigint PRIMARY KEY,
registrationnumber character varying(255));
INSERT INTO tmp_table
select id,registrationnumber from tableB;
for d in tmp_table loop
update TABLEA set registrationnumber=d.id where
registrationnumber=d.registrationnumber;
return next d;
end loop;
END;
What is going wrong with my script?
There's no reason to do this in a loop - let the database engine do it for you.
UPDATE tablea
SET registrationnumber = tableb.id
FROM tableb
WHERE tablea.registrationnumber = tableb.registrationnumber;
select * from tablea;
See this SQLFiddle:
http://sqlfiddle.com/#!1/1281b/1
Note that you are implicitly casting a varchar value to a bigint. If any of these varchar values don't cast correctly, the statement will fail.