Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 23rd, 2006, 07:15 PM
Authorized User
 
Join Date: Apr 2005
Location: Vancouver, , Canada.
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,

Reply With Quote
  #2 (permalink)  
Old January 23rd, 2006, 09:15 PM
Authorized User
 
Join Date: Sep 2005
Location: , , .
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/
Reply With Quote
  #3 (permalink)  
Old January 23rd, 2006, 10:42 PM
Authorized User
 
Join Date: Apr 2005
Location: Vancouver, , Canada.
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"--

Reply With Quote
  #4 (permalink)  
Old January 24th, 2006, 01:03 AM
Friend of Wrox
 
Join Date: Dec 2005
Location: , AZ, .
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
Reply With Quote
  #5 (permalink)  
Old January 24th, 2006, 11:10 PM
Authorized User
 
Join Date: Apr 2005
Location: Vancouver, , Canada.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Best Regards,
C

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02: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



All times are GMT -4. The time now is 11:58 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.