I want to get the results of this join query which are in the last 30 days.
V.[4501001]
contains the nvarchar dates in this format dd/mm/yyyy
Here is my query:
(select Distinct T.PId from [dbo].[TrialStatus] T inner join
form.TP_VISDATE V on T.pid = V.pid
where T.cid = 1064 and T.sid = 300 and (Convert(NVARCHAR, V.[4501001], 103) < Convert(NVARCHAR,GETDATE(),103)) and (Convert(NVARCHAR, V.[4501001], 103) > DATEADD(DD,-30,Convert(NVARCHAR,GETDATE(),103))))
If I use Convert(NVARCHAR,GETDATE(),103)
I get false results because this
select DATEADD(Day,-30,Convert(NVARCHAR,GETDATE(),103))
returns 2018-01-08 00:00:00.000
instead of the day 30 days before today
But if I use GETDATE()
as is without conversion I get this error:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Convert the value to a date, not a string!
select Distinct T.PId
from [dbo].[TrialStatus] T inner join
form.TP_VISDATE V
on T.pid = V.pid
where T.cid = 1064 and
T.sid = 300 and
Convert(date, V.[4501001], 103) < Convert(date, GETDATE()) and
Convert(date, V.[4501001], 103) > DATEADD(day, -30, Convert(date, GETDATE()));
Then, you can fix the data in your table. Don't store dates as strings. Store them using native types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.