  | 
              
	              
	                
  
    
	 
        Raw Query Laravel 5.6, SQL SERV ต้องการใส่เครื่องหมาย Single Quotes     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                Raw Query Laravel 5.6, SQL SERV 
ต้องการใส่เครื่องหมาย Single Quotes   between '2018-04-01' and  '2018-04-30'  เพราะถ้าไม่มีเครืองหมาย '  เวลา query  จะไม่มีข้อมูลออกมาเลย 
 
Code (PHP) 
แบบที่ 1
->whereBetween ('opdmas.VisitDate',[(convert(date,str_replace("*", "'",'*2018-04-01*'))),(convert(date,str_replace("*","'",'*2018-04-30*')))])
Error 
Call to undefined function App\Http\Controllers\Report\convert()"
 
 
Code (PHP) 
แบบที่ 2
->whereBetween ('opdmas.VisitDate',[str_replace("*", "'",'*2018-04-01*'),str_replace("*","'",'*2018-04-30*')])
Error 
Conversion failed when converting date and/or time from character string......
output:  [opdmas].[VisitDate] between '2018-04-01' and '2018-04-30'
 
 
Code (PHP) 
แบบที่ 3
->whereBetween ('opdmas.VisitDate',["'2018-04-01'","'2018/04/30'"])
Error
Conversion failed when converting date and/or time from character string.
output : opdmas].[VisitDate] between '2018-04-01' and '2018/04/30'
 
 
  Tag : PHP, Ms SQL Server 2016, Laravel  Framework               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2018-05-20 15:04:11 2018-05-20 15:04:42	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-05-20 15:03:12 | 
                      By :
                          nottpoo | 
                      View :
                          1686 | 
                      Reply :
                          10 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
$query->whereBetween('datetime',array('2018-05-01','2018-05-31 23:59:59'))
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-05-20 18:06:27              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-20 18:03:20 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2018-05-20 18:03:20 
รายละเอียดของการตอบ ::
  ต้องการระบุฟิล์ด ที่มันดึงมาแสดง  คือ master.VisitDate เพราะว่าข้อมูลวันที่นั้นได้มาจากการ join Table 
แต่ว่าการระบุ Array  มันจะยอมให้แค่ ค่าเดียว  แต่โค้ดที่พี่ ให้มา มันระบุแค่  datetime  แล้วตามด้วย Array  แล้วมันจะรู้เหรอคะว่าดึงมาจาก 
Table ไหน ถ้าเราไม่ระบุให้มัน 
  
 
ก็เลยลองเอาฟิลด์ master.VisitDate   ออก  มันก็ฟ้องทันทีเลยคะ Invalid column name 'datetime'  
Code (PHP) 
"SQLSTATE[42S22]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'datetime'.
  
 
ถ้าใส่แบบนี้ ->whereBetween('master.VisitDate','datetime',array('2018-04-01','2018-04-30')) 
Error  เพราะมันยอมได้แค่ค่าเดียวตาม   function whereBetween 
 
Code (PHP) 
	 public function whereBetween($column, array $values, $boolean = 'and', $not = false){
	 .
	 .
	}
	Type error: Argument 2 passed to Illuminate\Database\Query\Builder::whereBetween() must be of the type array, string given, called in D:\Apache24\htdocs\Laravel...on line 23
 
 
Code (PHP) 
public function TodayDetail()
    {
    //RAW QUERIES
	$TodayDetail = DB::table('DB_TEST.dbo.HNOPD_master  as master')
	->join('DB_TEST.dbo.PRESCRIP  as prescrip',
		function ($join){
	         $join->on('master.VN','=','prescrip.VN')
	                  ->on('master.VisitDate','=','prescrip.VisitDate');
	              })
	->join(
			 'DB_TEST.dbo.Cconfig  as config'
			 ,'prescrip.Clinic','=','config.code'
			 	)
	->select(DB::raw('count(master.VN) as VN','count(DISTINCT prescrip.Doctor) as Doctor')
			,'master.VisitDate'
			,'prescrip.Clinic'
			,'config.LocalName'
			)
	->where([['master.sxl','=','0'],['prescrip.Clinic','=',"'AO'"],['config.CtrlCode','=','12345']])
	->whereBetween('master.VisitDate','datetime',array('2018-04-01','2018-04-30'))
        ->get();
}
 
 
พอใส่แบบนี้ 
Code (PHP) 
->whereBetween('opdmas.VisitDate',array('2018-04-01','2018-04-30'))
 
 
ไม่ error  แต่ไม่มีข้อมูลออกมาตามรูป 
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-05-20 23:43:31              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-20 23:37:04 | 
                        By :
                            nottpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 datetime เป็นชื่อสมมติ แค่ให้รู้ว่าเป็น type datetime คุณก็เปลี่ยนให้ถูกต้องเอาเองครับ 
 
จุดสำคัญคือไม่ต้องไปใส่ quote ให้มัน มันใส่ให้เองอัตตโนมัติครับ 
 
ส่วนที่มันไม่มีค่าออกมา นี่ต้องดูว่า ข้อมูลมันถูกต้องไหม ลองให้มัน echo statement ออกมาเช็คด้วยตาก่อนว่า ถูกต้องไหม 
มี condition อะไรผิดเพี้ยนไปหรือเปล่า มีวงเล็บ ถูกต้องหรือไม่ 
ให้ลองใช้ ->toSql(); ดูครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 09:05:08 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตอบไม่ถูกเหมือนกันครับ ว่าทำไมไม่มี single quote ครอบมาด้วย เพราะที่เคยทำมามันมีออกมาด้วย 
 
อันนี้เป็นการทำแบบอ้อม แต่ไม่ใช่การแก้ปัญหาเรื่องการใช้ whereBetween  
 
->whereRaw(" your_fieldname between '2018-01-01' and '2018-01-31 23:59:59.999' ")->get();                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 14:21:11 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 config/database.php 
เปิด strict mode ใว้หรือเปล่าครับ เปลี่ยนเป็น false                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 15:19:42 | 
                        By :
                            DK | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 7 เขียนโดย : DK เมื่อวันที่ 2018-05-21 15:19:42 
รายละเอียดของการตอบ ::
  เปลี่ยนเป็น   false   แล้วคะ
  
 
อันนี้ที่ได้จาก  tosql(); 
Code (PHP) 
select count(mas.VN) as VN, [mas].[VisitDate], [prescrip].[Clinic], [config].[EnglishName], [config].[LocalName] as [LocalName] 
from [DB_TEST].[dbo].[MASTER] as [mas] inner join [DB_TEST].[dbo].[PRESCRIP] as [prescrip] on [mas].[VN] = [prescrip].[VN] 
and [mas].[VisitDate] = [prescrip].[VisitDate] inner join [DB_TEST].[dbo].[config] as [config] on [prescrip].[Clinic] = [config].[code] 
where ([mas].[cxl] = ? and [prescrip].[Clinic] = ? and [config].[CtrlCode] = ?) and [mas].[VisitDate] between ? and ? and [prescrip].
[Clinic] in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
group by [mas].[VisitDate], [prescrip].[Clinic], [config].[EnglishName], [config].[LocalName]
  
 
แต่พอใส่  
	//var_dump($sql->toSql()); 
	//var_dump($sql->getBindings()); 
เพื่อจะดูค่า ทำไม  error  "Call to a member function toSql() on string"                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-05-21 16:31:55 2018-05-21 16:41:26              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 16:30:53 | 
                        By :
                            nottpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 db::enableQueryLog(); 
 
ใส่ใว้ก่อนคิวรี่ 
 
dd(db::getQueryLog()); 
ใว้ล่างคิวรี่                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 19:48:24 | 
                        By :
                            DK | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ใช้ Eloquent เถอะครับ ชีวิตจะได้ดีขึ้น                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-21 19:52:24 | 
                        By :
                            DK | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |