Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

change data type from date or date/time to text

Is there a way to change any column in a set of tables from a data type of date or date/time to varchar using SQL?  

I can do it manually using Management Studio, but I thought there might an easier way since I have many date and/or date/time columns across multiple tables.
0
HLRosenberger
Asked:
HLRosenberger
  • 5
  • 5
1 Solution
 
LIONKINGCommented:
If you want to do all tables in one shot, you'd have to use dynamic SQL.
The idea would be to loop through the table names (i.e. sys.tables), then loop through the columns for those specific id's (i.e. sys.columns), you would filter out only the ones that are DateTime and then create a dynamic "ALTER TABLE...."

This is the first thing that comes to mind, maybe someone can find this already done somewhere out there, or has a better idea.
0
 
ralmadaCommented:
I actually don't understand why you would want to do that. date should be stored as date in sql not as varchar. I would strongly advise against changing the datatype.

Now if you insist, I guess you can do something like this



declare @sql varchar(max)
declare @tab varchar(255)
declare @col varchar(255)

DECLARE AlterTableCursor CURSOR FAST_FORWARD
FOR
SELECT table_name, column_name
FROM information_schema.columns
WHERE table_name NOT LIKE 'Temp_%' AND data_type = 'datetime'  
-- or you can define the set of tables like
-- WHERE table_name in ('Table1', 'Table2', 'Table3', etc...) AND data_type = 'datetime'  



DECLARE @AlterTableCmd VARCHAR(200)

OPEN AlterTableCursor

FETCH NEXT FROM AlterTableCursor INTO @tab, @col

WHILE @@FETCH_STATUS = 0
BEGIN
      SELECT @SQL = 'ALTER TABLE ' + @tab + ' ALTER COLUMN ' + @col + ' varchar(50) NOT NULL'
      EXEC (@AlterTableCmd)
      FETCH NEXT FROM AlterTableCursor INTO @tab, @col
END

CLOSE AlterTableCursor
DEALLOCATE AlterTableCursor
0
 
HLRosenbergerAuthor Commented:
Why would you advise against it?   Here's my situation - I have a screen scraper app.  I'm extracting data from various external WEB pages, pages I do not own.  The owner of the pages (a state government entity) has done a less than ideal job of data input validation.  Invalid dates may be, and have been, entered.   I'm storing these dates in a SQL database and I was using date datatype.  I do not want to validate all the dates, nor is it necessary on my part.  The bad dates are their problem.   I just need a copy of the data - bad dates and all.   So, if I use a date, SQL INSERT will fail.  If I treat the date data as text (varchar), my problem is solved.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ralmadaCommented:
>>So, if I use a date, SQL INSERT will fail.  If I treat the date data as text (varchar), my problem is solved. <<

Well, you might be fixing that problem, but you will have a lot more when you actually need to work with that data. say by filtering the data by date.
0
 
HLRosenbergerAuthor Commented:
I can always do an explicit conversion from varchar to date using SQL keyword/function, yes?
0
 
ralmadaCommented:
Yeah, but it will fail if you have bad data. Remember, Garbage in = More garbage out

Not to mention the overhead you're creating by doing the conversion everytime you run the query .
0
 
HLRosenbergerAuthor Commented:
understood.  I modified the SQL and executed it and none of the types changed.   I do not get any error.   here it is.   Do you see any issues?


declare @sql varchar(max)
declare @tab varchar(255)
declare @col varchar(255)

DECLARE AlterTableCursor CURSOR FAST_FORWARD
FOR
SELECT table_name, column_name
FROM information_schema.columns
WHERE table_name LIKE 'ihps_%' AND data_type = 'date'  

DECLARE @AlterTableCmd VARCHAR(200)

OPEN AlterTableCursor

FETCH NEXT FROM AlterTableCursor INTO @tab, @col

WHILE @@FETCH_STATUS = 0
BEGIN
      SELECT @SQL = 'ALTER TABLE ' + @tab + ' ALTER COLUMN ' + @col + ' varchar(20) NOT NULL'
      EXEC (@AlterTableCmd)
      FETCH NEXT FROM AlterTableCursor INTO @tab, @col
END

CLOSE AlterTableCursor
DEALLOCATE AlterTableCursor
0
 
ralmadaCommented:
The EXEC line is wrong, it should be

exec(@sqL)

declare @sql varchar(max)
declare @tab varchar(255)
declare @col varchar(255)

DECLARE AlterTableCursor CURSOR FAST_FORWARD
FOR
SELECT table_name, column_name
FROM information_schema.columns
WHERE table_name LIKE 'ihps_%' AND data_type = 'date'  


OPEN AlterTableCursor

FETCH NEXT FROM AlterTableCursor INTO @tab, @col

WHILE @@FETCH_STATUS = 0
BEGIN
      SELECT @SQL = 'ALTER TABLE ' + @tab + ' ALTER COLUMN ' + @col + ' varchar(20) NOT NULL'
      EXEC (@sql)
      FETCH NEXT FROM AlterTableCursor INTO @tab, @col
END

CLOSE AlterTableCursor
DEALLOCATE AlterTableCursor

Open in new window

0
 
HLRosenbergerAuthor Commented:
Also, this part is pulling back VIEWS.   I just want tables.

SELECT table_name, column_name
FROM information_schema.columns
WHERE table_name LIKE 'ihps_%' AND data_type = 'date'
0
 
ralmadaCommented:
SELECT a.table_name, a.column_name
FROM information_schema.columns a
inner join information_schema.tables b on a.table_name = b.table_name
WHERE b.table_name LIKE 'ihps_%' AND b.table_type = 'BASE TABLE' AND
a.data_type = 'date'
0
 
HLRosenbergerAuthor Commented:
Thanks so much!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now