Convert INT column to Datetime in SQL Server - sql

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.

Related

Convert entire column from integer to datetime using SQL

I have a table with an entire column that contains integers. I know that these integers were previously datetime values. I want to update all values in this column to be datetimes. There are 1000+ plus rows. I've altered this column from an INT to varchar since all the errors I received didn't like me going from an INT to DATETIME but I am having the same issue with the varchar data type.
Example values in the column: 43873, 40706, 43873, ect
I have tried the following queries:
UPDATE WORKER SET INT_TIME = DATEFROMPARTS(INT_TIME,1,1);
UPDATE WORKER SET INT_TIME = CONVERT(datetime,INT_TIME,107);
I receive errors like
Conversion failed when converting date and/or time from character
string.
INT_TIME is the column I am trying to update all values to a datetime:
These look like Excel format. That suggests something like:
select dateadd(day, col, '1899-12-30')
ALTER TABLE [dbo].[WORKER]
ALTER COLUMN [INT_TIME] DATETIME
This statement alters the integer column to a datetime column and parses the values automatically.
You can use dateadd() function :
select dateadd(day, t.int_time, 0) as joining_date
from tabel t;
You can use update statement to update , before that you need to alter table definition also :
alter table worker
alter column int_time DATETIME
update worker
set int_time = dateadd(day, t.int_time, 0)
0 have a default date 1900-01-01 00:00:00.000

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

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)

T-SQL computed date column

I have a table, in TSQL, with a field containing data in YYYYMMDD format saved as varchar(50);
I want to add a date type column to the table for each of the corresponding records in this field.
Any ideas?
Assuming that you have stored correct format of date in your field (eg you don't have '20121433'), this script should works for you:
ALTER TABLE your_table
ADD your_field_Date DATETIME
UPDATE your_table
SET your_field_Date = CONVERT(DATETIME, your_field_varchar, 112)
ALTER TABLE your_table DROP COLUMN your_field_varchar

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