','附近的语法不正确。 AND')'附近的语法不正确

时间:2019-01-15 06:23:22

标签: sql sql-server tsql

方括号有问题,我不知道错误在哪里。

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)
SET @theDate = GETDATE()
IF(CAST(DAY(@theDate)as int) > 9 ,SET @theDay = CAST(DAY(@theDate)as Varchar(6)), SET @theDay = '0' + CAST(DAY(@theDate)as Varchar(6)));
IF(CAST(DAY(@theDate)as int) > 9 ,SET @theMonth = CAST(MONTH(@theDate)as Varchar(6)), SET @theMonth = '0' + CAST(MONTH(@theDate)as Varchar(6)));

错误消息

Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ')'.

6 个答案:

答案 0 :(得分:0)

您使用了不适当的括号以及逗号。此外,我为节定义添加了set(UA_ENABLE_AMALGAMATION ON CACHE BOOL "" FORCE) set(UA_LOGLEVEL 300) add_subdirectory(open62541) add_dependencies(${PROJECT_NAME} open62541 open62541-amalgamation-source open62541-amalgamation-header) set (${PROJECT_NAME}_SRCS ${${PROJECT_NAME}_SRCS} "${PROJECT_BINARY_DIR}/open62541/open62541.c") begin。 这是您的工作代码

end

要查看输出,请将以下行添加到您的代码中

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)
SET @theDate = GETDATE()
IF(CAST(DAY(@theDate)as int)) > 9 
begin  
     SET @theDay = CAST(DAY(@theDate)as Varchar(6)) 
     set @theDay = '0' + CAST(DAY(@theDate)as Varchar(6)) 
end;
IF(CAST(DAY(@theDate)as int)) > 9 
begin 
     SET @theMonth = CAST(MONTH(@theDate)as Varchar(6)) 
     SET @theMonth = '0' +    CAST(MONTH(@theDate)as Varchar(6)) 
end;

答案 1 :(得分:0)

DECLARE @theDate varchar(60)

DECLARE @theDay varchar(6)

DECLARE @theMonth varchar(6)

SET @theDate = GETDATE()

 IF(CAST(DAY(@theDate)as int) > 9 )

  SET @theDay = CAST(DAY(@theDate)as Varchar(6))

 else

  SET @theDay = '0' +( CAST(DAY(@theDate)as Varchar(6)));

IF(CAST(DAY(@theDate)as int) > 9 )

 SET @theMonth = CAST(MONTH(@theDate)as Varchar(6))

else

 SET @theMonth = '0' + (CAST(MONTH(@theDate)as Varchar(6)));

请使用上述正确的其他语法。

答案 2 :(得分:0)

为什么不这样做呢?

select DAY(GETDATE())
select MONTH(GETDATE())

答案 3 :(得分:0)

也许您应该尝试此代码。

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)

SET @theDate = GETDATE()

IF
  CAST(DAY(@theDate)as int) > 9
THEN
  SET @theDay = CAST(DAY(@theDate)as Varchar(6))
ELSE
  SET @theDay = '0' + CAST(DAY(@theDate)as Varchar(6))
END

IF
  CAST(DAY(@theDate)as int) > 9
THEN
  SET @theMonth = CAST(MONTH(@theDate)as Varchar(6))
ELSE
  SET @theMonth = '0' + CAST(MONTH(@theDate)as Varchar(6))
END

答案 4 :(得分:0)

在sql查询中使用if时,最好在if循环后使用Begin和End。 您可以尝试一下。

IF(CAST(DAY(@theDate)as int)> 9) 开始 SET @theDay = CAST(DAY(@theDate)as Varchar(6)); SET @theDay ='0'+ CAST(DAY(@theDate)as Varchar(6)); 结束 IF(CAST(DAY(@theDate)as int)> 9) 开始 SET @theMonth = CAST(MONTH(@theDate)as Varchar(6)); SET @theMonth ='0'+ CAST(MONTH(@theDate)as Varchar(6)); 结束 结束

答案 5 :(得分:0)

正如其他人所说,这不是条件语句的语法。应该是:

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)
SET @theDate = GETDATE()

IF CAST(DAY(@theDate)as int) > 9 BEGIN
  SET @theDay = CAST(DAY(@theDate)as Varchar(6))
END
ELSE BEGIN
  SET @theDay = '0' + CAST(DAY(@theDate)as Varchar(6))
END
IF CAST(DAY(@theDate)as int) > 9 BEGIN
  SET @theMonth = CAST(MONTH(@theDate)as Varchar(6))
END
ELSE BEGIN
  SET @theMonth = '0' + CAST(MONTH(@theDate)as Varchar(6))
END

您尝试使用条件函数,您具有IIF函数,这是其语法:

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)
SET @theDate = GETDATE()

SET @theDay = IIF(CAST(DAY(@theDate)as int) > 9, 
                  CAST(DAY(@theDate)as Varchar(6)), 
                  '0' + CAST(DAY(@theDate)as Varchar(6)));
SET @theMonth = IIF(CAST(DAY(@theDate)as int) > 9, 
                    CAST(MONTH(@theDate)as Varchar(6)), 
                    '0' + CAST(MONTH(@theDate)as Varchar(6))); 

顺便说一句,用前导零填充的最简单方法是使用格式函数:

DECLARE @theDate varchar(60)
DECLARE @theDay varchar(6)
DECLARE @theMonth varchar(6)

SET @theDate = GETDATE()
set @theDay = Format(DAY(@theDate), '00')
set @theMonth = Format(MONTH(@theDate), '00')