t-sql 2012 give column name and format date field

  • In t-sql 2012 I have a select statement that looks like the following:

    Select CONVERT(smalldatetime,CS.value,101) AS [MM/DD/YYY].

    My problem is that I want to call this field [Beginning Date]. I need to also format the date in mm/dd/yyyy format.

    I can not get the field formatted in mm/dd/yyyy format and call the field [Beginning Date].

    Thus can you show me the t-sql to solve my problem?

  • What datatype is CS.Value ?

    If datetime/smalldatetime then simply

    SELECT CONVERT(varchar(10),CS.Value,101) AS [BeginningDate]

  • Try this out for a size. Note, the statement you have doesn't do any formatting, in literal terms it means "convert into smalldatetime from a source that has 101 format.

    😎

    USE tempdb;

    GO

    SET NOCOUNT ON;

    ;WITH DATE_SET AS

    ( SELECT CAST(DATE_VAL AS DATETIME) AS DATE_VAL FROM (VALUES

    ('2014-09-27 13:22:34.593')

    ,('2014-09-28 23:30:34.593')

    ,('2014-09-24 17:06:34.593')

    ,('2014-09-29 00:34:34.593')

    ,('2014-09-24 17:06:34.593')

    ,('2014-10-01 19:46:34.593')

    ,('2014-09-24 18:10:34.593')

    ,('2014-09-27 14:26:34.593')

    ,('2014-09-26 04:18:34.593')

    ,('2014-09-30 09:38:34.593')

    ) AS X(DATE_VAL)

    )

    SELECT

    CONVERT(VARCHAR(10),DS.DATE_VAL,101)

    FROM DATE_SET DS;

    Results

    ----------

    09/27/2014

    09/28/2014

    09/24/2014

    09/29/2014

    09/24/2014

    10/01/2014

    09/24/2014

    09/27/2014

    09/26/2014

    09/30/2014

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply