Datepart(year, …) vs. Year(…)

后端 未结 2 661
太阳男子
太阳男子 2021-01-04 00:12

What are the advantages of using one over the other in the following:

DATEPART(YEAR, GETDATE())

As opposed to:

YEAR(GETDATE         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 00:24

    There is no difference. In the execution plan both is translated to as datepart(year,getdate()).

    This is true for SQL Server 2005, 2008 and 2012.

    select datepart(year, getdate())
    from (select 1 x) x
    
    select year(getdate())
    from (select 1 x) x
    

    Execution plan.

    
    
      
        
          
            
              
              
                
                  
                    
                  
                  
                    
                  
                  
                    
                      
                        
                          
                            
                              
                                
                                  
                                    
                                  
                                  
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        
        
          
            
              
              
                
                  
                    
                  
                  
                    
                  
                  
                    
                      
                        
                          
                            
                              
                                
                                  
                                    
                                  
                                  
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        
      
    
    

提交回复
热议问题