Solved

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

Posted on 2006-06-20
7
834 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

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.

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 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