Add YYYY-MM column to existing table - sql

Using SQL Server Management Studio - all date columns in date format.
Table name: dbo.[FP Data]
Date column (YYYY-MM-DD): order_date
New date column (YYYY-MM): order-month
I used this query:
SELECT
*,
FORMAT(order_date,'YYYY-MM') AS order_month
FROM
dbo.[FP Data]
to create a column in the format YYYY-MM. I now want to merge the new column with my table.
(I need in a YYYY-MM format to compare to other data I have in a YYYY-MM format)
Happy to scrap the above if there is a cleverer way to add the YYYY-MM column OR find a way to name the new column as a table and somehow merge the two tables.

Create a computed column:
alter table FPdata add order_month AS FORMAT(order_date,'yyyy-MM')
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ad93628258b6c674d7403239f0744803

You may be able to use the "Alter Table" function to solve your question!
ALTER TABLE table_name
ADD column_name data_type column_constraint;

Related

How to convert a string column into a date column

Imported a date column from a CSV file where there are string values are shown as
date
44705
44704
44703
I want to convert the entire column into a date format as
date
"2022-05-22"
"2022-05-21"
"2022-05-20"
I have used this script which allowed me to generate the result.
SELECT dateadd(d,44703,'1899-12-30') as date
The question is, how could I apply this script for a range of values (there are 700 rows in the table). e.g 44000 to 44705. I would like all string values to be converted as a date format.
select cast (44705-1 as smalldatetime) gives 2022-05-25 00:00
So you could just update the column using the above.
See https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=dc24abb3025e0f3796e7d978ba406be3
New fiddle with update statement, this line will update all rows, as per test.
update #test
set pdate = cast(dateadd(d,tdate-2,'1899-12-30') as smalldatetime)
To convert a string to date we can add temporary date column and fill it and delete old column and rename new one to old one
alter table TableName add NewColumnName date null; --create temporary column
update TableName set NewColumnName =dateadd(d,cast(cast([date] as float) as int)-2,'1899-12-30') --fill it
alter table TableName drop column [date]--delete old column
EXEC sp_RENAME 'TableName.NewColumnName' , 'date', 'COLUMN'--rename new one to old one

SQL - How to format date in column

The dates provided in the Expiration Date column appear as just numbers, ie 09192019, I would like the entire column to be in a date format ie; 09/19/2019
Code:
SELECT Expiration_Date
FROM Insurance
datatype of the table column is probably not date. I suggest you to fix the datatype by setting it to date by altering your table.
for this, use the below steps:
alter table Insurance add column (expiration_date_new date);
update Insurance set expiration_date_new = TO_DATE(Expiration_Date, 'MM/DD/YYYY');
alter table Insurance drop column expiration_Date;
alter table Insurance rename column expiration_date_new to expiration_date;
as a workarround you can convert Expiration_date column to date by
SELECT TO_DATE(Expiration_Date, 'MM/DD/YYYY') FROM Insurance

Alter column of type timestamp

I want to update an empty table , which has a column of type timestamp to varbinary(8)
I used the following command
ALTER TABLE Notification ALTER COLUMN RowRevisionID varbinary(8)
and I get and an error
Cannot alter column 'RowRevisionID' because it is 'timestamp'.
How can I change a timestamp column type?
I do not wish to drop the column an add a new one , because that will create a column at the end , and I wish to preserve column order to use this table in an INSERT INTO
You unfortunately cannot make a change to a timestamp column, as the error implies; you are stuck with what you have. Also, each table can only have one timestamp column, so you cannot duplicate the column in any solution.
Your best bet (depending on the size of the table) might be to copy the data into a staging table (using SELECT * INTO MyTempTable FROM OriginalTable syntax to preserve the timestamp values), then drop and recreate the original table with the desired columns in the desired order and reinsert the data, or you could add a new VARBINARY(8) column to the existing table and drop the timestamp column, preserving the original table. There are probably other solutions along the same lines as these, but all will require a new column, rather than an ALTER COLUMN script.
You are looking for:
ALTER TABLE Notification DROP RowRevisionID;
and
ALTER TABLE Notification ADD RowRevisionID varbinary(8) AFTER myOtherColumn;

Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE

I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
ORA-00905: missing keyword
I have also tried :
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
ORA-01735: invalid ALTER TABLE option
However when i try to add a fresh column with DATE datatype it works fine.
example :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
It's the first one, with a slight modification:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not sure that will work for those particular datatypes and it also may not work depending on the current data.
You may find it necessary in that case to:
create a new column X of date type.
populate X based on the old column (may need several passes of data fix-ups to work).
delete old column.
rename X to old column name.
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
Here's my solution and it works perfectly.
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
modify a column then syntax is:-
alter table table_name modify column_name datatype;
but when you modify the column datatype column must be empty
Thanks for the hints! This got it for me.
alter table Table_Name
Alter column Column_Name datatype
GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME COLUMN new_col TO old_col;
alter table employee add (DOB varchar(10));
if you add a column with datatype varchar and if you want to modify the datatype of DOB then you can use this command ->
alter table employee modify(DOB date);
Now the table is modified.

How to combine columns within a table (Access SQL)

There are three columns of date information (month, day, year) in a table and I would like to create a new column in the table and combine those 3 columns into a the new column so that I can have a single formatted date. How can I do this? I know how to query this but I am trying to figure out how to copy this information to the original table. Thanks in advance.
If I understood well, you need to alter your table and add a datetime column
You need to do something like this
UPDATE YourTable
SET DATECOLUMN = '#' & DAYCOLUMN & '/' & MONTHCOLUMN & '/' & YEARCOLUMN & '#'
It was the sintaxis in access if i remember it well.
Greetings.
There are a number of way of managing such a data scrubbing exercise e.g.
CREATE a new table including a new NOT NULL column and omitting the now-redundant columns, INSERT..SELECT into the new table only the data you require, DROP the old table (you'll end up with a different table name which may not be a bad thing).
ADD a new nullable column to the existing table (or NOT NULL if you have an appropriate DEFAULT), UPDATE the new column, alter the column to make it NOT NULL then DROP the now-redundant columns.