locked
Leading Zero for Month RRS feed

  • Question

  • User-1193791088 posted

    This is probably a simple request

    DECLARE @JobDay AS CHAR (8) = CONVERT (CHAR (4), DATEPART(YEAR, @DateTimeStart)) + CONVERT (CHAR (2), DATEPART(MONTH, @DateTimeStart)) + CONVERT (CHAR (2), DATEPART(DAY, @DateTimeStart));

    It returns 2017 218

    what i would like is 20170218

    Thursday, October 19, 2017 12:47 AM

All replies

  • User2103319870 posted

    what i would like is 20170218

    You can try with below function like below

    DECLARE @JobDay AS CHAR (8) =CONVERT (CHAR (4), DATEPART(YEAR, @DateTimeStart)) +   CONVERT(VARCHAR(2), @DateTimeStart, 101) + CONVERT (CHAR (2), DATEPART(DAY, @DateTimeStart));
    Thursday, October 19, 2017 1:46 AM
  • User452040443 posted

    Hi,

    RobertH3, try:

    DECLARE @JobDay AS CHAR (8) = CONVERT (CHAR (8), @DateTimeStart), 112);

    Hope this help

    Thursday, October 19, 2017 5:21 PM
  • User77042963 posted
    DECLARE @JobDay AS CHAR (8) 
    
    Select @JobDay=Format(getdate(),'yyyyMMdd')
    print @JobDay
    
    
    DECLARE @JobDay2 AS CHAR (8) 
    
    Select @JobDay2=Replace(Convert(varchar(10),getdate(),120),'-','')
    print @JobDay2

    Thursday, October 19, 2017 6:51 PM