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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Covert datetime to varchar

Author  Topic 

tmeers
Starting Member

1 Post

Posted - 2007-11-11 : 10:49:50
Hello all,
I am in a bit of a spot, I need to pull a date from a database but it's a 'last used' time. Well if the row has never been used it has no date. So I have a query that is isnull(LastUsed, 'Not Used') But you cannot convert a datetime to a character string so easily I guess because it will not work.

I have been searching high and low for an answer but all I can find is how to convert varchar to datetime not the other way around.

If any one knows how to do this let me know the closest I got which is still wrong was:
isnull(LastUsed, CONVERT(datetime, CONVERT(varchar(10), 'Not Used')))

Thanks a million,
Tim

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-11 : 10:58:03
why don't you detect NULL on your front end application and display the 'Not Used' there ?

if you want to do this in T-SQL,
isnull(convert(varchar(10), LastUsed, 121), 'Not Used')




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-11 : 11:07:05
quote:
Originally posted by tmeers

Hello all,
I am in a bit of a spot, I need to pull a date from a database but it's a 'last used' time. Well if the row has never been used it has no date. So I have a query that is isnull(LastUsed, 'Not Used') But you cannot convert a datetime to a character string so easily I guess because it will not work.

I have been searching high and low for an answer but all I can find is how to convert varchar to datetime not the other way around.

If any one knows how to do this let me know the closest I got which is still wrong was:
isnull(LastUsed, CONVERT(datetime, CONVERT(varchar(10), 'Not Used')))

Thanks a million,
Tim



High and low? with a few exceptions like google or search option at this site. The below link shows lots of articles and posts about dealing with datetime in SQL.

http://www.sqlteam.com/search.aspx?cx=011171816663894899992%3Aaow51lf_dim&cof=FORID%3A9&q=convert+date+to+string&sa=Search#1164
Go to Top of Page
   

- Advertisement -