Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to cast date in dd-mm-yyyy format on fetching columns from a table in dynamic Sql Query. I am fetching columns,data type from a master table and fetching and all done as a dynamic query

What I have tried:

SELECT @columns = COALESCE(@columns+',' ,'') +
 CASE WHEN ColumnType='DateTime'
 THEN  CONVERT(VARCHAR(24),ColumnName,103)
 ELSE
 CAST(ColumnName AS Varchar) END
 FROM ICCS_StdReport_Column_Master where TableId=@ip_tableId

 SELECT @tableName=TableName FROM ICCS_StdReportTable_Master where TableId=@ip_tableId



 SET @SQL = ' WHERE  convert(varchar(30),Download_Date,111)
 between '+ ''''+ convert(varchar(30),@ip_FromDate,111) +''''+'  AND '+
 ''''+ convert(varchar(30),@ip_ToDate,111) +''''



 set @Query=
 'SELECT  '+@COLUMNS+'
 FROM '+@tableName+ @SQL
Posted
Updated 1-Mar-17 4:46am

1 solution

If you want it in a specific format then you need to choose that format. See SQL Server CONVERT() Function[^
Your code is converting using style 111 so just change it to be the one you want.
 
Share this answer
 
Comments
[no name] 1-Mar-17 23:28pm    
I need to get the columns with datatype datetime in dd/mm/yyyy format fetched from the table dynamically . In my table there is ColumnName,Columndatatype

Below is the core part
CASE WHEN ColumnType='DateTime'
THEN CONVERT(VARCHAR(24),ColumnName,103)
ELSE
CAST(ColumnName AS Varchar) END

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900