Solved

change data type from date or date/time to text

Posted on 2013-01-10
11
442 Views
Last Modified: 2013-01-10
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
Comment
Question by:HLRosenberger
  • 5
  • 5
11 Comments
 
LVL 13

Expert Comment

by:LIONKING
ID: 38764091
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
 
LVL 41

Expert Comment

by:ralmada
ID: 38764147
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
 
LVL 1

Author Comment

by:HLRosenberger
ID: 38764356
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
 
LVL 41

Expert Comment

by:ralmada
ID: 38764412
>>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
 
LVL 1

Author Comment

by:HLRosenberger
ID: 38764440
I can always do an explicit conversion from varchar to date using SQL keyword/function, yes?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 41

Expert Comment

by:ralmada
ID: 38764495
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
 
LVL 1

Author Comment

by:HLRosenberger
ID: 38764623
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
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38764633
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
 
LVL 1

Author Comment

by:HLRosenberger
ID: 38764649
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
 
LVL 41

Expert Comment

by:ralmada
ID: 38764683
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
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 38764734
Thanks so much!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now