Solved

SQL Datetime to varchar in 'M/d/yyyy' format

Posted on 2006-06-20
7
791 Views
Last Modified: 2008-01-09
Hello experts,

How would I go about converting a datetime value into a date of format 'M/d/yyyy' (i.e. no zeros so: 1/1/2006 or 11/20/2006)?

Thanks
0
Comment
Question by:TLevin10
7 Comments
 
LVL 13

Assisted Solution

by:Atlanta_Mike
Atlanta_Mike earned 150 total points
ID: 16945316
One way is:

SELECT datepart(mm,DateField) + '/' + datepart(dd,DateField) + '/' + datepart(yyyy,DateField)
FROM TableName

Another is:
SELECT REPLACE(convert(varchar(15),DateField,101),'0','')
FROM TableName

You'll have to test to see which is most efficient for your process.
0
 

Author Comment

by:TLevin10
ID: 16945871
Actually, neither one is correct...

The first one has problems concatenating int and string types, while the second one cuts out ALL the zeros so '10/20/2005' becomes '1/2/25' which is clearly incorrect...

However, your first one did remind me that I could do it manually using the basic datepart functions.  The correct solution is below, but I'm hoping that maybe there is a better solution somewhere?

Yes, I know this is the correct answer, I'm wondering if there is something better/shorter/easier, since I have to do this multiple times for different fields

=================
SELECT CONVERT(varchar(2),datepart(mm,DateField)) + '/' + CONVERT(varchar(2),datepart(dd,DateField)) + '/' + CONVERT(varchar(4),datepart(yyyy,DateField))
FROM TableName
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16947641
This is best accomplished using your front-end application and not rely on SQL Server's rudimentary formatting.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:TLevin10
ID: 16947667
actually, this is going to be done in a C# RowFilter on the client side - thats why the formatting has to be handled as such - I built a "search" tool which operates on a similar principal to programs like iTunes, which filters dynamically on all columns (including datetime columns) - right now, it correctly matches dates only if they have zeros in them (such as '6/03/2006') becuse of the way the CONVERT function works (the C# rowfilter operates on the same principals as a SQL 'where' statement.  The code I am using right now is:

dataView1.RowFilter = string.Format("CONVERT(title, System.String) LIKE '*{0}*') OR (CONVERT(dateOrdered, System.String) LIKE '*{0}*') OR (CONVERT(dateDue, System.String) LIKE '*{0}*'", userWord);

Hope that helps explain why I'm looking for this type of formatting and need it to be short...
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 100 total points
ID: 16949313
some thing like this


select cast(month(getdate()) as varchar) +'/'+cast(day(getdate()) as varchar)+'/'+cast(year(getdate()) as varchar)



select cast(month(DateField) as varchar) +'/'+cast(day(DateField) as varchar)+'/'+cast(year(DateField) as varchar)
from yourTable
0
 

Author Comment

by:TLevin10
ID: 16950550
thats the same as the original answer I gave, just using cast instead of convert, and the shorthand notations for datepart, but thanks :)
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 16969203
Damn... you're right... forgot the cast..and my brain was missing in the second... duh.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

863 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

27 Experts available now in Live!

Get 1:1 Help Now