Solved

sql date format

Posted on 2011-09-26
8
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 143

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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