Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2004, 10:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert DateTime

What is the easiest way to extract only the date from a datetime field?
I want to be able to group by a date in a datetime field and ignore the time part.

I've tried to use left(datetime, 11) but it reformats the date as MMMM dd ccyy (the original format was mm/dd/ccyy). To be honest, the format that I would prefer is either ccyy/mm/dd or ccyy.mm.dd.

Do I need to write a function to get this date format?

Rand
__________________
Rand
 
Old October 4th, 2004, 10:56 AM
Authorized User
 
Join Date: Jul 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I created a function to convert my AS400 DB2 dates,

CREATE FUNCTION [ufn_AS400Date] (@InDate datetime)
RETURNS varchar(10) AS
BEGIN
    RETURN LEFT(CONVERT(VarChar(20), @InDate, 120), 10)
END

Check out the Convert function in BOL.


David


 
Old October 4th, 2004, 12:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,

It does exactly what is needed!

:D

Rand
 
Old October 5th, 2004, 09:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can use this instead.
Code:
CONVERT(VarChar(10), getdate(), 120)
without having to extract the 10 characters from the left, after conversion.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert String to DateTime SimoNWellborne C# 3 October 11th, 2014 02:24 AM
Convert DateTime Format dnyanubhamare C# 2005 2 February 25th, 2008 09:06 AM
How to convert a datetime into a binary... gbianchi SQL Language 2 September 10th, 2007 09:43 AM
How to convert a string in DateTime elena SQL Language 6 March 29th, 2006 06:59 PM
How to convert a string in DateTime elena ADO.NET 3 July 24th, 2003 04:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.