How to Change the Data Type of a Column Using T-SQL
Submitted by admin on Saturday, January 29, 2011 - 23:47.
There are some cases that you need to change the data type of a field in SQL Server by not using the Management Studio Tools. The reason is if your database is replicated. You cannot change it directly using the management studio tools so your best option is to use T-SQL.
In my case I have a column name “TransDate” that has a datetime datatype. Now when I create a report and filter the record using the date column I cannot filter it easily by using a value like:
"{qry_rpt_SalesReportPerItem.TransDate} IN '" & Format(dtpBegDate.Value, "MM/dd/yyyy") & "' TO '" & Format(dtpEndDate.Value, "MM/dd/yyyy") & "'"
This could return an error “A date-time is required here.”
So the solution is to change the data type from datetime to date.
To do this all you have to do is create a “New Query” file and run this code using the ALTER TABLE statement:
ALTER TABLE invoice ALTER COLUMN TransDate date
In this case you do not need to drop the column in your table.
Add new comment
- 17 views