Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fun With Dates

Status
Not open for further replies.

greese

IS-IT--Management
Nov 6, 2003
13
0
0
I am exporting the date along with results from my table my table but need it in the format yymmdd 12/17/2003 becomes 031203.

To test this out, i have the following:
Code:
Declare @dtYear varchar(2)
Declare @dtMonth varchar (2)
Declare @dtDay varchar (2)

set @dtYear = right (Datepart(yyyy, Getdate()+22),2)
set @dtmonth = Datepart (mm, Getdate()+22)
set @dtDay = datepart (dd, Getdate()+22)

Select @dtYear as [Year],
 @dtMonth as [Month],
 @dtDay as [Day];
My results are:
Code:
Year Month Day  
---- ----- ---- 
04   1     8
how do i get the leading zero to drop in there for the date? When i concatenate it together i get 0418 instead of 040108

I have tried searching for something on this but came up empty.

Any ideas are greatly appreciated.

G Reese
 
Try:-

set @dtmonth=right("00" + Datepart (mm,Getdate()+22),2)

Dickie Bird (:)-)))
 
This will simplify your code and give you the result you are looking for:
Code:
Select CONVERT(char(6),GetDate()+22,12)

~Brian
 
For yymmdd format, try:-

select convert(char(6),getdate(),12)
 
perfect.

Except i made a mistake. It was supposed to be mmddyy

Does the same method handle that as well?

Thanks!

G Reese
 
Check out the CONVERT function in Books Online. The 12 I used in my CONVERT function is the style property. There are a whole bunch of styles to choose from. I think that you will want to use a style of 10. THis leaves you with mm-dd-yy. You can use the Replace function to get rid of them.
Code:
Select REPLACE(CONVERT(varchar(8),GetDate()+22,10),'-','')

~Brian
 
Thanks! That did the trick and i think am now on the right path.

I knew there had to be an easier way then how i was doing it.

G Reese
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top