Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 convert varchar to date

Author  Topic 

ramya_rav2000
Starting Member

2 Posts

Posted - 2005-11-18 : 15:47:47
Hi

I have a varchar field with values like "Jun 13 1995 12:" I want to change the type of the field to Date and the format of the values to "mm/dd/yyyy".

If I change the type of the field in the design view to Date, SQL gives me an error saying that the type cannot be changed and data would be lost.

So I think changing the dates to the mm/dd/yyyy format first and then changing the field type from varchar to date would work.

Can someone please help me as how to do this.

Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-18 : 16:21:29
DECLARE @dt datetime
SELECT @dt = 'Jun 13 1995 12:00:00'
SELECT CONVERT(varchar(10),@dt,101)


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

ramya_rav2000
Starting Member

2 Posts

Posted - 2005-11-18 : 16:59:10
Hi,

Thanks for your reply. Jun 13 1995 12: is an example of one data in the field. I want to change the format of all the data in all the records to mm/dd/yyyy. Is there a way I can do this ? There are 100's of records in the table.

Thanks
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2005-11-18 : 17:48:48
I think you're going to have to pull out the individual pieces of the string using the string functions. I'd look in Books Online for SUBSTRING. You should be able to use it to craft a string that SQL Server will convert to a date.

Or SELECT CAST(LEFT('Jun 13 1995 12:', 11) AS DATETIME) works fine on my machine.

===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-18 : 23:50:17
Hereafter dont use varchar or any other data type to store dates. DATETIME data type is specifically for storing Dates and dont worry on which format to store it. Let SQL Server stores it in its way and when you show the data in presentation layer format it there to the format you want to format

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

spinoza
Starting Member

49 Posts

Posted - 2006-01-07 : 11:02:39
ÇÉ all,

I have the following query from an asp page...

select ARTICLE.title, ARTICLE_RATING.comments, ARTICLE_RATING.rating, PORTAL_MEMBERS.m_name, ARTICLE_RATING.rate_date
from ARTICLE, ARTICLE_RATING, PORTAL_MEMBERS
where article.article_id= article_rating.article
AND ARTICLE_RATING.rate_by=PORTAL_MEMBERS.member_id
order by m_name

The field ARTICLE_RATING.rate_date is stored date values but in my database has a nvarchar property...
It is stored like that 20060107213345 where the first 4 digits are the year the other two the month... the date (07) and the time (213345)...

I need to represent it through the query as a Date... Can someone help me how can trnform the above query???
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-09 : 01:22:16
Use Proper DateTime data type to store Dates
Here is one of the methods


declare @n nvarchar(20)
set @n='20060107213345'
select cast(left(@n,8)+' '+substring(@n,9,2)+':'+substring(@n,11,2)+':'+substring(@n,13,2) as datetime) as NewDate


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -