Solved

date convert function

Posted on 2003-12-05
8
342 Views
Last Modified: 2012-06-22
in SQL server 7, how can we change text data type to datetime type? like: 01/01/01 is text, i waant to chnge to 01/01/2001 as date data in a table. Thanks
0
Comment
Question by:snowingnow
8 Comments
 
LVL 6

Accepted Solution

by:
lausz earned 30 total points
ID: 9883717
You can try something like this



select yourfield, case when right(yourfield, 2) > 10 then substring(yourfield,1,6) + '19' +  right(yourfield, 2)  else
substring(yourfield,1,6) + '20' +  right(yourfield, 2)  end
from yourtable
0
 
LVL 6

Expert Comment

by:lausz
ID: 9883746

And to insert this value in a datetime field ..

create newtable( field1 varchar(10), field2 datetime)

insert into newtable
select yourfield, case when right(yourfield, 2) > 10 then convert(datetime,substring(yourfield,1,6) + '19' +  right(yourfield, 2),103)  else
convert(datetime,substring(yourfield,1,6) + '20' +  right(yourfield, 2) ,103) end
from yourtable
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9883996
@snowingnow:

> in SQL server 7, how can we change text data type to datetime type? like:
> 01/01/01 is text, i waant to chnge to 01/01/2001 as date data in a table. Thanks

You should just be able to use the CONVERT function.  Like this:
     CONVERT(DATETIME, '01/01/01')

In order to explain it any better, I need to know what you want to do with it.

Hope That Helps,
Dex*
0
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.

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9884139
you want to change the column type?

go into enterprise manager and
select the table.... right click
select design mode
highlight the relevant column
change the datatype to DATETIME.
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 9886855
I think you can change the datatype while having data as varchar
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 9886858
sorry You >> can't
0

Featured Post

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

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

860 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