SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert into varchar
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Johnph
Yak Posting Veteran

95 Posts

Posted - 09/28/2012 :  10:21:16  Show Profile  Reply with Quote
Hello, I am having some difficulty with some sql. I want to convert datetime into varchar. On the database it is displayed as datetime like this:

1999-01-01 00:00:00.000

My code looks like this:

Declare @Date varchar(200)

SELECT @date = Date from table

SELECT LEFT(CONVERT(VARCHAR,@date , 130), 11)


This grants me this output:
Jan 1 1999


As you can see from the output, it makes a double space between "Jan" and "1". Is there a way I can make it say "Jan 1 1999" rather than "Jan 1 1999" or perhap if there is a way to put the actual value of the datetime into a varchar "1999-01-01 00:00:00.000"



Edited by - Johnph on 09/28/2012 10:22:36

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 09/28/2012 :  10:25:15  Show Profile  Reply with Quote
try

Declare @Date datetime
SELECT @date = Date from table

SELECT CONVERT(varchar(25),@date,121)

i didnt understand why you want to change datatype though. keep in mind that using this value again for some date manipulations would require a convertion back to date datatype

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/28/2012 :  10:25:42  Show Profile  Reply with Quote
You can remove the double spaces using replace:
SELECT REPLACE(LEFT(CONVERT(VARCHAR,@date , 130), 11),'  ',' ');
Also, depending on what you want to get, you can use a different style isntead of using 130. All the styles are listed her: http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

All that said, most experts advise that you do this type of formatting at the presentation layer such as reporting services, or GUI client application, if you have one.
Go to Top of Page

Johnph
Yak Posting Veteran

95 Posts

Posted - 09/28/2012 :  10:38:56  Show Profile  Reply with Quote
Hey guys, thanks so much!
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 10/05/2012 :  05:04:46  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Johnph

Hello, I am having some difficulty with some sql. I want to convert datetime into varchar. On the database it is displayed as datetime like this:

1999-01-01 00:00:00.000

My code looks like this:

Declare @Date varchar(200)

SELECT @date = Date from table

SELECT LEFT(CONVERT(VARCHAR,@date , 130), 11)


This grants me this output:
Jan 1 1999


As you can see from the output, it makes a double space between "Jan" and "1". Is there a way I can make it say "Jan 1 1999" rather than "Jan 1 1999" or perhap if there is a way to put the actual value of the datetime into a varchar "1999-01-01 00:00:00.000"





The extra space is reserved to make a room for extra digit (2 digit days). For day part from 10 to 31 you wont get two sapces

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000