SQL - update now() data with only date - sql

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)

Related

How to return date format (f.e. mm/dd/yyyy) from a date (f.e. 06/30/2019)?

I'm working on data validation in the SQL Server Database.
I need to check if dates in the Table are populated in a valid format for specified Country.
Is there any function in SQL that will return a date format from a date?
For example: Date: 06/30/2019 What I need to get: mm/dd/yyyy
Thanks!
As we've all mentioned in the comments, storing a date as a varchar in the database is the wrong idea. We also now know that all dates need to be the last day of the month (from the comments), so now we do have something to work with. Therefore, you could fix your table with something like the below:
--Create a new column to store old values
ALTER TABLE YourTable ADD DateString varchar(10);
--Update the new column and change the existing column to ISO (yyyyMMdd) format
UPDATE YourTable
SET DateSting = DateColumn,
DateColumn = TRY_CONVERT(varchar(10), TRY_CONVERT(date, DateColumn, 101), 112);
--Change the data type of the datecolumn
ALTER TABLE YourTable ALTER COLUMN DateColumn date;
--Inspect "lost" data:
SELECT DateString
FROM YourTable
WHERE DateColumn IS NULL
AND DateString IS NOT NULL;
GO
--Add the CHECK CONSTRAINT
ALTER TABLE YourTable
ADD CONSTRAINT Date_EOMonth CHECK (DateColumn = EOMONTH(DateColumn));
If the CHECK CONSTRAINT fails to be created you have dates that aren't at the end of the month, so you can use the below statement to inspect them, or the statement after to update them all to the end of the month:
SELECT DateColumn
FROM YourTable
WHERE DateColumn != EOMONTH(DateColumn);
--Blanket Update
UPDATE YourTable
SET DateColumn = EOMONTH(DateColumn);

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 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;

Update table to add a literal value to a column

I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql

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