在select语句中转换日期格式

时间:2014-08-03 10:16:45

标签: sql-server tsql date type-conversion

我正在尝试将输出中的日期转换为dd-mm-yyyy格式。我曾尝试仅仅转换日期,但是如果仅在select语句显示输出时如何转换日期。

例如,这是我的查询

select * 
from dbo.Car 
where 
    dbo.Car.[Date Registered] > DATEADD(year, -3, GetDate()) 
    and dbo.Car.rentalCost < 350 
ORDER by 
    [Date Registered] desc, brand asc;

上述语句Output

的当前输出

在上面的查询中,我选择了所有内容,其中一列是DateRegistered这是一个日期列,那么我该怎么做才转换日期列?我应该在select之后指定每一列吗?

select 
   column1, column2, column3,
   Convert(varchar(10), CONVERT(date, Datecolumn4, 105), 103)...

有更简单的解决方案吗?

2 个答案:

答案 0 :(得分:1)

我创建了这个动态创建列列表的SP,并将所有具有Datetime和Date数据类型的列转换为dd-mm-yyyy。至少它会有助于手动编写大量的转换。

CREATE PROCEDURE usp_ColumnToStringDate(@p_TableName SYSNAME, @p_FilterString VARCHAR(4000) = NULL)
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @TableName SYSNAME
    DECLARE @ColumnName SYSNAME
    DECLARE @SchemaName SYSNAME
    DECLARE @DataType SYSNAME
    DECLARE @StartLoop INT
    DECLARE @EndLoop INT
    DECLARE @SQLColumnList VARCHAR(4000)
    DECLARE @SQL VARCHAR(4000)
    DECLARE @FilterString VARCHAR(4000)


    SET @TableName = @p_TableName
    SET @FilterString = @p_FilterString

    DECLARE @TargetTable TABLE (
                OrderID INT IDENTITY(1, 1),
                TableSchema SYSNAME,
                TableName SYSNAME,
                ColumnName SYSNAME,
                DataType SYSNAME
            )

    INSERT INTO @TargetTable
    SELECT   QUOTENAME(c.TABLE_SCHEMA),
             QUOTENAME(c.TABLE_NAME),
             QUOTENAME(c.COLUMN_NAME),
             UPPER(c.DATA_TYPE)
    FROM     INFORMATION_SCHEMA.COLUMNS c
    WHERE    c.TABLE_NAME = @TableName
    ORDER BY c.ORDINAL_POSITION

    --SELECT * FROM @TargetTable

    SELECT   @StartLoop = MIN(tt.OrderID),
             @EndLoop        = MAX(tt.OrderID),
             @SchemaName     = MIN(tt.TableSchema),
             @TableName      = MIN(tt.TableName)
    FROM     @TargetTable tt

    WHILE @StartLoop <= @EndLoop
    BEGIN
        SELECT   @DataType = tt.DataType,
                 @ColumnName = tt.ColumnName
        FROM     @TargetTable tt
        WHERE    tt.OrderID = @StartLoop

        --PRINT @ColumnName+' '+@DataType

        SET @SQLColumnList = ISNULL(@SQLColumnList, '')
            + CASE 
                   WHEN @DataType IN ('DATETIME', 'DATE') THEN CHAR(9) + CHAR(9)
                        + 'CONVERT(VARCHAR(10),' + @ColumnName + ', 105)'
                   ELSE CHAR(9) + CHAR(9) + @ColumnName
              END

        IF @StartLoop != @EndLoop
        BEGIN
            SET @SQLColumnList = @SQLColumnList + ',' + CHAR(13) + CHAR(10)
        END

        IF @StartLoop = 1
        BEGIN
            SET @SQLColumnList = 'SELECT' + REPLACE(@SQLColumnList, CHAR(9), ' ')
        END


        SET @StartLoop = @StartLoop + 1
    END
    SET @SQL = @SQLColumnList + CHAR(13) + CHAR(10) + 'FROM ' + @TableName + CHAR(13) + CHAR(10)
    SET @SQL = COALESCE(@SQL+@FilterString,@SQL)
    PRINT @SQL
END 

<强>用法:

--SELECT Statement
EXEC usp_ColumnToStringDate 'ProductListPriceHistory',NULL

--SELECT Statement with Filter
EXEC usp_ColumnToStringDate 'ProductListPriceHistory','WHERE ProductID = ''1'''

--Use in other SP or assign to parameters
DECLARE @DSQL VARCHAR(MAX)

EXEC @DSQL =  usp_ColumnToStringDate 'ProductListPriceHistory','WHERE ProductID = ''1'''
PRINT REVERSE(SUBSTRING(REVERSE(@DSQL),2,LEN(@DSQL)))

答案 1 :(得分:0)

我认为最好在select语句中指定每个列名,并使用日期列:

select col1, col3, col3, CONVERT(varchar(10),[Date Registered], 105), col5, col6, col7
from dbo.Car 
where dbo.Car.[Date Registered] > DATEADD(year, -3, GetDate()) and  dbo.Car.rentalCost < 350 
ORDER by [Date Registered] desc, brand asc;