简体   繁体   English

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

[英]Incorrect syntax near ','. AND Incorrect syntax near ')'

I have some problem with brackets and I do not know where the error is.我有一些括号问题,我不知道错误在哪里。

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)));

Error Message错误信息

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 ')'.
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)));

Please use proper if else syntax as above.请使用正确的 if else 语法,如上所述。

You have used inappropriate parenthesis as well as commas.您使用了不适当的括号和逗号。 Moreover I have added begin and end for section definition.此外,我为部分定义添加了beginend Here is your working code这是您的工作代码

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;

To see the output add below lines to your code要查看输出,请将以下几行添加到您的代码中


select @theDay
select @theMonth

Why not simply do it like this:为什么不简单地这样做:

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

Maybe you shoul try this code.也许你应该试试这个代码。

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

When you are using if in sql query,it is better to use Begin and End after the if loop.在 sql 查询中使用 if 时,最好在 if 循环后使用 Begin 和 End。 You can Try this.你可以试试这个。

IF(CAST(DAY(@theDate)as int) > 9) Begin SET @theDay = CAST(DAY(@theDate)as Varchar(6)); IF(CAST(DAY(@theDate)as int) > 9) 开始 SET @theDay = CAST(DAY(@theDate)as Varchar(6)); SET @theDay = '0' + 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));结束 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)); SET @theMonth = '0' + CAST(MONTH(@theDate)as Varchar(6)); END END结束 结束

As others have said, that is not the syntax for the conditional statement.正如其他人所说,这不是条件语句的语法。 It should be:它应该是:

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

You tried to use a conditional as a function, you have the IIF function to do so, this is its syntax:您尝试将条件用作函数,您有 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))); 

By the way, the easiest way to padding with leading zeros is to use the format function:顺便说一下,用前导零填充的最简单方法是使用 format 函数:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM