?
Solved

sql server 2008 cast

Posted on 2013-10-24
6
Medium Priority
?
360 Views
Last Modified: 2013-10-25
How can I use the CAST function to put 1 digit months as a 1 digit with leading zero's

   such as January should be 01
                Feburary should be 02
                March should be 03  etc.

here is the code I am using:
        cast( datepart (MM,XHR_Applicant.ApplicationDate) as varchar (2)) castMM,

               Thanks for your help  

                Jerry
0
Comment
Question by:jvonhendy
[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
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39598707
RIGHT( '00'+cast( datepart (MM,XHR_Applicant.ApplicationDate) as varchar (2)) ,2) as castMM
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39598710
You mean something like this?


declare @datein sysname
declare @date datetime
set @datein = '03252013'
set @date = substring(@datein,1,2)+'/'+substring(@datein,3,2)+'/'+substring(@datein,5,4)

SELECT Convert(VarChar(8), Cast(@date As DateTime), 112)
0
 
LVL 9

Expert Comment

by:COANetwork
ID: 39598724
SELECT RIGHT('0'+ CONVERT(VARCHAR,datepart (MM,XHR_Applicant.ApplicationDate)),2) AS castMM

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39599545
just limit the return of any date style using MM at the start to 2 characters:

convert(char(2),XHR_Applicant.ApplicationDate,1) AS castMM,

the 1 use above is the 'date style'
see: SQL Server Date Styles (formats) using CONVERT()
0
 
LVL 5

Expert Comment

by:MohitPandit
ID: 39599890
Hello,

Please check below code with CAST with sample data

SELECT
	castMM =
		CASE
			WHEN LEN([Month]) = 1 THEN '0'+ CAST([Month] AS VarChar(2))
		ELSE
			CAST([Month] AS VarChar(2))
		END
FROM
(
	SELECT [Month] = DATEPART(MM,'2013-1-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-2-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-3-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-4-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-5-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-6-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-7-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-8-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-9-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-10-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-11-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-12-25 14:45:14.293')
) T1 -- Your input table

Open in new window


Please note, you may use CONVERT function as well with below code:

SELECT
	castMM =
		CASE
			WHEN LEN([Month]) = 1 THEN '0'+ CONVERT(VarChar(2),[Month])
		ELSE
			CONVERT(VarChar(2), [Month])
		END
FROM
(
	SELECT [Month] = DATEPART(MM,'2013-1-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-2-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-3-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-4-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-5-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-6-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-7-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-8-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-9-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-10-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-11-25 14:45:14.293')
	UNION ALL
	SELECT [Month] = DATEPART(MM,'2013-12-25 14:45:14.293')
) T1 -- Your input table

Open in new window


Kindly take a look over it and let me know in case you have any concern.

Best Regards,
Mohit Pandit
0
 

Author Closing Comment

by:jvonhendy
ID: 39600439
It worked PERFECTLY!!  And excellent explanation of why 1 is used.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

765 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