Copying data from one column with different data type to another with different datatype

If I have a table that has a column for storing date (name entryDate) ( it’s datatype is datetime) and another column name value (data type is varchar(400)). Right now value column has just Y or N for each row. However, I want to copy all the dates from entryDate column to the value column and i’m okay with the Y or N getting overwritten.

I’m using MS SQL Server 2012. Is the following SQL is what I need to accomplish my task?

UPDATE table_name 
SET entryDate = value

What type have the value column?

This?

1 Like

What is the point of duplicating your data? What is the real problem you’re trying to solve by doing this?

1 Like

Yes.

image

I want to do a comparison with the copied value of dates inside the value column with today’s date and if the difference is more than one year, then I want to display a checklist for the user to complete on the front end. If it’s within or less than 1 year from today’s date, then I don’t want to display the checklist to the user. Basically I want to keep showing the checklist if it’s older than one year when they last completed it.

Why do you need to duplicate the data to check it against today’s date?

That was decided by someone else so I’m proceeding with that. Coming back to my original question - is the SQL query I mentioned good enough to duplicate the data?

I would suggest to this person that it makes absolutely no sense to do this, especially for the given purpose.
There is a data type for dates for reasons, so you can make queries conditional of the date, which is exaclty what you would use to do what you describe.
So duplicating date data to a non-date format in order to do date comparison defies all logic.

Try it and see what happens. Back up the table first, incase the result is unexpected.

what happened when you tested it? ™

co-sign

Double-sign

Classic XY Problem. And when told it is wrong still insists on doing it wrong instead of asking how to do it right.

Triple sign.

There are infinitely better ways to do it, both from a performance standpoint but from an accuracy standpoint, if you use the tools as they are meant to be used, and not doing some goofy conversions that make no sense whatsoever.

UPDATE table_name
SET value = entryDate

fiddle

the hero we all need but do not deserve

please check the fiddle – it takes only a few minutes to create working SQL to demonstrate a concept, and i wish more people knew about it

thank you, @sibertius

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.