user2195741
user2195741

Reputation:

Compare datetime in query sql server

I have multiple field comparison and the only one that fails is the datetime field. Here my query:

SELECT 
       [RPC_NUMERO_CHEQUE]
      ,[RPC_NUMERO_CUENTA_ORIGEN]
      ,[RPC_ESTATUS_PAGO]
      ,[RPC_CODIGO_OFICINA]
      ,[RPC_FECHA_CHEQUE]
  FROM [Filemaker_CheckPro].[dbo].[TBL_RESUMEN_PROCESO_CAMARAS]
  WHERE [RPC_NUMERO_CHEQUE]='0025' 
     AND [RPC_NUMERO_CUENTA_ORIGEN]='07101000376' 
     AND [RPC_ESTATUS_PAGO]=1 
     AND [RPC_CODIGO_OFICINA]=1 
     AND [RPC_PROCESS_DATE] BETWEEN 20120911 AND 20120912//<--- HERE

As you can notice, it does not work but I need to compare those field too and it should returns 1 or more results. So, how can i archive it?

Upvotes: 0

Views: 89

Answers (2)

Taryn
Taryn

Reputation: 247610

Your current query is very close. Since you have a string value for the date then you need single quotes around the date value:

AND [RPC_PROCESS_DATE] BETWEEN '20120911' AND '20120912'

Upvotes: 1

Sachin
Sachin

Reputation: 40970

You can try with Cast function of Sql like this

 AND [RPC_PROCESS_DATE] BETWEEN 
      CAST('20120911' as datetime) AND 
      CAST('20120912' as datetime)

Upvotes: 1

Related Questions