Solved

Format Date in SQL Stored Procedure

Posted on 2008-06-20
4
904 Views
Last Modified: 2008-07-17
Hello, I have a simple question.  

How do I format a date in a stored procedure that selects some records from a table?   I need the format to be like this:

06/19/2008 0:00:00

Usually, I get the date value like this: 06/19/2008 12:00:00 AM.  I am exporting these records through a dataset in a .NET application to a text file and I  need the date format to be exacly what I need.  I don't need the 12 or the AM/PM label.  Also, the formatted date has to be a datetime field and not a text or varchar.

Thanks!
0
Comment
Question by:TheUndecider
  • 2
4 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21834335

select cast(convert(varchar(10), getdate(), 101) as datetime)
0
 

Author Comment

by:TheUndecider
ID: 21834643
Hello, thanks for replying.   I tried your solution and I still get the date as before: 6/20/2008 12:00:00 AM.  I still get the time as 12:00:00 AM.  

We used to export from a MS Access query to a comma delimited text file and the format Access does for date is mm/dd/yyyy h:mm:ss.  I need to recreate the same format but still keeping the date as a datetime field.  

Right now the closest I've got is to do it like this:
(convert(varchar,mydatefield)) + ' 0:00:00'

However, this makes it a text field and when exporting to the text file this field gets enclosed with quotation marks and it should not.


0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21834674
I think it has to do w/ the transfer from the db to your interface code because what I posted shows 2008-06-20 00:00:00.000 in the database for me.
0
 
LVL 4

Accepted Solution

by:
Thunder724 earned 500 total points
ID: 21849709
select convert(varchar(10), getdate(), 101) as [datetime]
This will return 06/23/2008
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Solution for warm standby SQL server 20 38
Vb. Net application freezes 9 30
SQL Query stumper 3 36
MS SQL Inner Join - Multiple Join Parameters 2 19
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

910 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

24 Experts available now in Live!

Get 1:1 Help Now