Solved

sql date format

Posted on 2011-09-26
8
215 Views
Last Modified: 2012-05-12
I have a copy of some code i need to adjust. the following code sets the date as 160611
i need to change the date to the following format 20110417.

please ignore the actual dates as they are just examples.
also with my print statement nothing happens.
set dateformat dmy 

--***********************************
--Declare Variables
--***********************************
declare 
	 @Date varchar(10)
	 , @FileDate varchar(8)


SET @FileDate = CONVERT(VARCHAR,
					CASE WHEN DATEPART(DD,@Date) < 10 
						 THEN '0' + CONVERT(VARCHAR,DATEPART(DD,@Date))
						 ELSE CONVERT(VARCHAR,DATEPART(DD,@Date))
					END
				 )
				+ CONVERT(VARCHAR,
					CASE WHEN DATEPART(MM,@Date) < 10 
						 THEN '0' + CONVERT(VARCHAR,DATEPART(MM,@Date))
						 ELSE CONVERT(VARCHAR,DATEPART(MM,@Date))
					END
				 )
				+ CONVERT(VARCHAR,RIGHT(DATEPART(YYYY,@Date),2))


print @FileDate

Open in new window

0
Comment
Question by:aneilg
8 Comments
 
LVL 5

Accepted Solution

by:
DerZauberer earned 250 total points
ID: 36598380
--***********************************
--Declare Variables
--***********************************
declare 
	 @Date varchar(10)
	 , @FileDate varchar(8)


SET @FileDate = CONVERT(VARCHAR,DATEPART(YYYY,@Date))
				+ CONVERT(VARCHAR,
					CASE WHEN DATEPART(MM,@Date) < 10 
						 THEN '0' + CONVERT(VARCHAR,DATEPART(MM,@Date))
						 ELSE CONVERT(VARCHAR,DATEPART(MM,@Date))
					END
				 )
				+ CONVERT(VARCHAR,
					CASE WHEN DATEPART(DD,@Date) < 10 
						 THEN '0' + CONVERT(VARCHAR,DATEPART(DD,@Date))
						 ELSE CONVERT(VARCHAR,DATEPART(DD,@Date))
					END
				 )


print @FileDate

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36598476
If @date was a datetime data type you can just do this:

SET @FileDate = select CONVERT(nvarchar(10), @Date, 112)
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36598802
set @filedate=convert(char(8),@date,112)
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:aneilg
ID: 36598813
thanks guy.
one last question why does my print @FileDate not work.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36598874
It does work. It prints to the messages tab. Use:

Select @FileDate

This returns it as a recordset.
0
 

Author Comment

by:aneilg
ID: 36598882
silly me. thanks.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36599032
btw, here my article to handle date/time correctly:
http://www.experts-exchange.com/A_1499.html
0
 

Author Closing Comment

by:aneilg
ID: 36923375
thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with duplicate records in Oracle query 16 24
Count with a subquery showing details 10 40
export sql results to csv 6 34
TSQL query to generate xml 4 31
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now