我可以使用Alias作为过滤器吗? T-SQL 2012

时间:2014-08-29 15:34:40

标签: sql sql-server tsql compiler-errors sql-server-2012

我想和

做同样的事
 /*This code works fine in mysql Android*/
            SELECT id, client, num, fs, tus,
                         convert(datetime, fs, 103) AS Fecha,
                         SUBSTRING( fs, 9, 2) AS day,
                         SUBSTRING( fs, 6, 2) AS month, 
                         SUBSTRING( fs, 1, 4) AS year
                         FROM table 
                         WHERE fs <> 'NULL' 
                         AND mes = '07'
                         AND anio = '2014'
                         AND dia = '11'
                         ORDER BY dia ASC;

  /*But in SQL is different a cause of datetime and here is where i cant make it work*/
          SELECT 
           convert(datetime, fs, 103) AS fs,
           datePart(mm, convert(datetime, fs, 103)) AS month ,
           datePart(yyyy, convert(datetime, fs, 103)) AS year,
           datePart(dd, convert(datetime, fs, 103) AS day)
           FROM table
           WHERE day <> 'NULL'

我不知道如何将fs,月,年和日作为表来操纵它们

1 个答案:

答案 0 :(得分:0)

如果您想将字段与NULL进行比较,只需使用完整字段:

WHERE fecha_servicio is not null;

如果该值可以是'NULL',那么您可以尝试:

WHERE fecha_servicio <> 'NULL';

但是,SQL引擎可能会在进行过滤之前评估select,在这种情况下,您将在转换时收到错误。如果您使用的是SQL Server 2012+,请使用try_convert()代替convert()