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
Related
I have a column having dates in varchar format, I want to convert it, to date format, which I did through below query:
select to_date(EVENT_DT, 'YYYYMMDD') from demographic;
Now, I want to create a new column and populate the column with the values that I got from above query. I am not able to do it with alter table.
Any solutions?
Hope it works as #Mike Walton said
Alter table demographic add Converted_date date;
----ms sql server
Update demographic set Converted_date = convert(date, EVENT_DT , 101);
---or using to_date
Update demographic set Converted_date = to_date(EVENT_DT, 'YYYYMMDD');
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
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;
we need to alter the table column data type from string to date. While am trying to do am getting the below error. Could you please help.
hive> describe sales_staging;
OK
cust_id string prod_num string
qty int sale_date string
sale_id string
Time taken: 0.151 seconds,
Fetched: 5 row(s)
hive> alter table sales_staging CHANGE sale_date sale_date DATE ;
FAILED: Execution Error, return code 1 from
org.apache.hadoop.hive.ql.exec.DDLTask. Unable to alter table. The
following columns have types incompatible with the existing columns in
their respective positions :sale_date
hive>
You can't give same name to column you wish to change datatype of. use like this
ALTER TABLE sales_staging CHANGE sale_date sale_date_new DATE;
refer this Apache Hive Wiki
you can't change the existing string type data to date type. but we can able to solve this issues in 2 ways.
create another table with the same columns count but the data type is date where the column you need string to date, then use insert command to export old table data to new table by casting the string to date.
add a new column to existing table with datatype as date. overwrite the table itself by casting the string to date into the new column.
ex:
I have orders table
describe orders;
order_id int
order_date string
order_customer_id int
order_status string
created another table ordersnew
describe ordersnew;
id int
odate date
cid int
ostatus string
now exported the orders data to ordersnew table
insert into ordersnew select order_id,cast(from_unixtime(unix_timestamp(substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss')) as timestamp) as strdate, order_customer_id,order_status from orders;
substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss' this is the place you have to check and alter your query as per your data.
please check here for date conversions
Do the following steps:
step-1) check all the dates in field "sale_date" are valid dates or not. If yes then go to step-2
step-2) Check the date format, for DATE datatype format should be 'yyyy-MM-dd'. If the date format is 'yyyy-MM-dd HH:mm:ss' then you should try the following syntax:
alter table sales_staging CHANGE sale_date sale_date TIMESTAMP;
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.