Solved

date convert function

Posted on 2003-12-05
8
340 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
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.

 
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:dishanf
ID: 9886855
I think you can change the datatype while having data as varchar
0
 
LVL 8

Expert Comment

by:dishanf
ID: 9886858
sorry You >> can't
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

910 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

21 Experts available now in Live!

Get 1:1 Help Now