Happy
Happy

Reputation: 1145

How to use between in stored proc in SQL Server

I need to use between in a stored procedure. This is my code

ALTER PROCEDURE sp_tb_sales_entry_total_product_where_date
   @to_date varchar(15),
   @from_date varchar(15)
AS
BEGIN
    SELECT * 
    FROM tb_sales_entry_total_product 
    WHERE [Sales Date] BETWEEN @to_date AND @from_date
END

I passed two values 01-01-2014 and 10-01-2014. In my table also in same format.

Problem is its not selecting values. Where is my error?

Upvotes: 2

Views: 217

Answers (3)

royb
royb

Reputation: 713

use sql format..

DATE_FORMAT(colName,\'%d/%m/%Y\') AS colName

you can use the format you want.. link

Upvotes: -1

juergen d
juergen d

Reputation: 204766

Switch them

WHERE [Sales Date] BETWEEN  @from_date AND @to_date

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Operator BETWEEN in SQL has the following syntax:

test_expression [ NOT ] BETWEEN begin_expression AND end_expression

Since the begin_expression needs to be first, you have to switch the order of from_date and to_date:

SELECT * FROM tb_sales_entry_total_product WHERE [Sales Date] BETWEEN @from_date AND @to_date 

Upvotes: 7

Related Questions