Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 January 23rd, 2006, 08:15 PM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting a date format to dd-Mon-yyyy

Is there a convert function that can be used to change the format of a date from 6/27/2005 to 27-Jun-2005 ?

This is what I am using:

select (datepart(dd,dateinsp) )
& '-'& DateName(Month,(datepart(Month,dateinsp)))
& '-'& Datepart(yyyy,dateinsp)
from Date_cleanup

I've tried various methods of using the cast as Varchar and the Convert(varchar(50),dateinsp) but continually get the following error

Syntax error converting the nvarchar value 'January' to a column of data type int.

Thanks all,

 
Old January 23rd, 2006, 10:15 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT REPLACE(CONVERT(VARCHAR,GETDATE(),106),' ','-')

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old January 23rd, 2006, 11:42 PM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

" I sense lack of friends in you" if you think that was a funny statement think again. Not all programmers created the mess they are in. In most cases it's a matter of cleaning up the mess of others before them. If you don't have anything positive to add then keep your comments to yourself. If you would have read my post thoroughly you would have seen that your recommendation was pretty much useless.

"May you drop of the earth"--

 
Old January 24th, 2006, 02:03 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

Cole,

Problem # 1: & is the bitwise AND operator and can only be used on integers, it is not the concatenation operator which is +

Problem # 2: DatePart returns an int and the second parameter of datename is expecting a DateTime data type or something that is implictly castable to a datetime type. You would be better using DateName and forget DatePart

Problem # 3: You did not tell us whether dateinsp column is varchar or datetime

Problem # 4: SQLMenace did give you a working solution (assuming that dateinsp is of type datetime)

Problem # 5: SQLMenance is apparently a big Star Wars fan and his "useless updates" quote is his way of mocking/honoring/imitating Yoda, and it is his signature, he puts it on every post and not just this one. It is nothing personal.

Problem # 6: You don't attack people that are trying to help you.

Here is your solution:

-- How to take this 6/27/2005 and make it 27-Jun-2005

CREATE TABLE Date_Cleanup
    (dcID int IDENTITY(1,1)
    ,dtDateInsp DATETIME
    ,vchDateInsp varchar(50)
)

INSERT Date_Cleanup (dtDateInsp, vchDateInsp) VALUES ('1/4/05','1/4/05')
INSERT Date_Cleanup (dtDateInsp, vchDateInsp) VALUES ('2/4/2005','2/4/2005')
INSERT Date_Cleanup (dtDateInsp, vchDateInsp) VALUES ('12/4/05','12/4/05')
INSERT Date_Cleanup (dtDateInsp, vchDateInsp) VALUES ('11/24/05','11/24/05')
INSERT Date_Cleanup (dtDateInsp, vchDateInsp) VALUES ('10/24/2005','10/24/2005')

-- Cole's code reworked
select DATENAME(dd,dtDateInsp)
    + '-'+ DateName(Month,dtDateInsp)
     + '-'+ DateName(yyyy,dtDateInsp)
,DATENAME(dd,vchDateInsp)
    + '-'+ DateName(Month,vchDateInsp)
     + '-'+ DateName(yyyy,vchDateInsp)
from Date_cleanup

-- SQLMenace Example -- expanded
SELECT REPLACE(CONVERT(VARCHAR,dtDateInsp ,106),' ','-')
, REPLACE(CONVERT(VARCHAR,CAST(vchDateInsp as DATETIME),106),' ','-')
FROM Date_cleanup

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old January 25th, 2006, 12:10 AM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks David, your time and your advice is much appreciated.

Best Regards,
C






Similar Threads
Thread Thread Starter Forum Replies Last Post
Date dd-mm-yyyy John2112 SQL Server 2000 2 March 16th, 2007 05:15 AM
Converting short date to show mm/dd/yyyy crabjoe Access 4 March 7th, 2007 03:46 AM
Converting date to yy.mm.dd format using CONVERT Jinn SQL Server 2000 6 October 14th, 2006 06:44 PM
Date formats... dd/mm/yyyy SeanW Classic ASP Databases 3 June 14th, 2004 12:34 PM





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