Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Syntax for shortening Datetime

Posted on 2008-10-21
3
Medium Priority
?
269 Views
Last Modified: 2012-05-05
I have a Column in a table that stores the date.  The formating on it is datetime.  I pull from this column for a description that I put in another table.  The description includes other thing in the string so I store it as varchar.  Problem is when I cast the datetime as varchar to store it has the date 10/21/2008 12:00AM.  I don't need the time on there but not sure how to get rid of it since I'm casting it as a varchar.
0
Comment
Question by:Nathan Riley
3 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 700 total points
ID: 22770547
convert(varchar,ColName,101)

This should do it...convert a DateTime field named ColName to a varchar using style 101
See this table:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 27

Assisted Solution

by:MikeToole
MikeToole earned 700 total points
ID: 22770555
What you need is the TSQL Convert() function which takes a third parameter for dates to specify the style of the date. E.g. for US dates:
select convert(varchar(10), getdate(),101)
Gives:
10/21/2008
 
0
 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 600 total points
ID: 22770874
since you've had the string, do this

select cast( cast('10/21/2008 12:00AM' as varchar(10)) as datetime)

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Viewers will learn how the fundamental information of how to create a table.

876 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