我想要实现这样的功能我该怎么做。
DECLARE @filterByDate VARCHAR(20)
IF(@filterByDate = 'expectedDate')
SET @filterByDate = 'ExpectedStartDate'
ELSE
SET @filterByDate = 'ActualStartDate'
SELECT @filterByDate AS Date
FROM TableName
感谢。
答案 0 :(得分:3)
SQL Standard,适用于大多数RDBMS
SELECT CASE @filterByDate
WHEN 'ExpectedStartDate' THEN ExpectedStartDate
WHEN 'ActualStartDate' THEN ActualStartDate
ELSE NULL
END as mydate
--optional , @filterByDate AS ColumnName
FROM
MyTable
答案 1 :(得分:1)
DECLARE @filterByDate VARCHAR(20)
IF(@filterByDate = 'expectedDate')
SET @filterByDate = 'ExpectedStartDate'
ELSE
SET @filterByDate = 'ActualStartDate'
Declare @Sql varchar(max)
SET @SQL = 'SELECT @Filter AS DATE FROM TableName'
DECLARE @ParmDefinition nVARCHAR(20)
SET @ParmDefinition = N'@filter nvarchar(20)'
exec sp_executesql @SQL, @ParmDefinition, @Filter = @filterByDate