Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert into varchar

Author  Topic 

Johnph
Posting Yak Master

103 Posts

Posted - 2012-09-28 : 10:21:16
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"


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-28 : 10:25:15
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-28 : 10:25:42
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
Posting Yak Master

103 Posts

Posted - 2012-09-28 : 10:38:56
Hey guys, thanks so much!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-10-05 : 05:04:46
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
   

- Advertisement -