Solved

change data type from date or date/time to text

Posted on 2013-01-10
11
439 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

8 Experts available now in Live!

Get 1:1 Help Now