Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to get record of all rows from last 30 to todays. i have tried this but isn't working

What I have tried:

select date_time from dsr_data where date_time >= CONVERT(Varchar(100), DATEADD(DAY,-30,GETDATE()),103) and date_time <= CONVERT(varchar(100), getdate(),103)
Posted
Updated 3-Feb-22 2:09am
Comments
Santosh kumar Pithani 31-Oct-17 4:36am    
You have to convert column "date_time <= CONVERT(varchar(100), getdate(),103)" on bothsides in given condition.

SELECT date_time  FROM dsr_data
    WHERE date_time
               between DATEADD(DAY,-30,GETDATE()) and  GETDATE(); 
 
Share this answer
 
Why are you using CONVERT?
The only possible reason is the same reason it doesn't work: you are storing your dates as NVARCHAR columns instead of DATE, DATETIME, or DATETIME2.

When you compare strings, the comparison is based entirely on the first different character in the two strings: and since you are specifically selecting to format the date in 103 format, which is "dd/MM/yyyy" the comparison will be total junk - the year will be checked last for example, so 31/01/2017 will be before 31/12/1952 because the comparison will be based on the first digit on the month, and '0' is less than '1'

Change your database. Always store data in appropriate datatypes: that means integer values in INT columns, date information in DATE, DATETIME, or DATETIME2 columns, and so on. If you don't, this is just the first of many nasty, nasty problems waiting to bite you down the line...
 
Share this answer
 
Comments
ADI@345 31-Oct-17 3:14am    
now i change nvarchar to datetime , and using this query but didn't work.

select date_time from dsr_data where date_time >= DATEADD(DAY,-30,GETDATE()) and
date_time <= getdate()
OriginalGriff 31-Oct-17 3:30am    
I would use BETWEEN, but assuming your data in the table is correct, your query should work. - it does when I run it against my data (with the table and column names changed).
So start by looking at your data: Assuming "didn't work" means it returned no rows, comment out the "AND date_time ..." part so it's returning everything for thirty days again, and see what you get.
ADI@345 31-Oct-17 4:13am    
here on datetime data save as YYYY-MM-DD
but on getdate() format i get as YYYY-DD-MM
CHill60 31-Oct-17 4:43am    
It doesn't matter how the date displays - when it is stored as a datetime it is just that - a date + time. So using between will still work
ADI@345 31-Oct-17 4:13am    
i thought this is the problem ...
Use below query.

SELECT date_time  FROM  dsr_data
WHERE date_time  >= DATEADD(day,-30, getdate()) 
and   date_time  <= getdate()
 
Share this answer
 
Comments
ADI@345 31-Oct-17 3:20am    
i am using the same code but didn't work
Atlapure Ambrish 31-Oct-17 3:42am    
This should work, I have checked it and is working for me. What is the datatype of date_time column?
ADI@345 31-Oct-17 3:52am    
CREATE TABLE [dbo].[dsr_data](
[client_type] [varchar](100) NULL,
[date_time] datetime NULL,
[tme] [varchar](100) NULL,
[area] [varchar](100) NULL,
[company_name] [varchar](100) NULL,
[client_name] [varchar](100) NULL,
[mobile] [varchar](100) NULL,
[email] [varchar](100) NULL,
[address_name] [varchar](100) NULL,
[landmark] [varchar](100) NULL,
[curr_presence_appoint] [varchar](100) NULL

) ON [PRIMARY]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900