Solved

change data type from date or date/time to text

Posted on 2013-01-10
11
447 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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.​
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

837 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