Question

hi guys i have column that is Nvarchar(50) that i use it as a date type column because i use Persian calendar now i want to select times from two different time for example show me data s that are between 2012/12/18 and 2012/12/20 how could i do that please help and by the way i want use Stored Procedures for these actions i try this but it dose not return any thing

ALTER proc [dbo].[sel_monthReport] @fdate nvarchar(50),@sdate nvarchar(50),@para   nvarchar(50)
as
begin
declare  @parametr nvarchar(50)
set @parametr=@para
select * from main where @parametr>=@fdate and @parametr <=@sdate
end
Was it helpful?

Solution

Let's say you have a column in your main table called RecordDate, and this column is of type nvarchar, with the date format yyyy/mm/dd.

In that case, you don't need the @para parameter in your stored procedure. Change it to something like this:

ALTER PROC [dbo].[Sel_monthreport] @fdate NVARCHAR(50), 
                                   @sdate NVARCHAR(50) 
AS 
  BEGIN 
      SELECT * 
      FROM   main 
      WHERE  main.recorddate BETWEEN @fdate AND @sdate 
  END 

Note, that this is a NVARCHAR comparison, ant it only works because your dates are stored in the format yyyy/mm/dd. In general, a better approach would be to compare DATETIME datatypes (better performance and less error-prone).

You can convert an NVARCHAR value to a DATETIME datatype using the CONVERT function: http://msdn.microsoft.com/en-us/library/ms187928.aspx

OTHER TIPS

A similar question has been asked on StackOverflow before. As you note, out of the box, SQL Server does not support Persian. In the examples on the links on this answer (How to convert datetime in persian in sql server), the date is stored in the database as SQL Server date format, but converted back/forward to Persian date format. You could extend these UDF examples to instead be datetime format.

Another alternative would be to write a CLR date/time function to do the same. There's an example on CodeProject. Either way, it's probably not best to store the dates in SQL Server as text format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top