Solved

change data type from date or date/time to text

Posted on 2013-01-10
11
446 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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