I need to run an query/create filter, within a program, every day WHERE last_paid is TODAY - 30 and TODAY = convert(varchar,getdate(),101). The biggest issue seems to be the inflexiblity of the program to have today be anything other than convert(varchar,getdate(),101).
This is going to be a short answer because this is an incredibly easy problem. You want to use DATEADD your syntax should be something like TODAY = convert(varchar,DATEADD(DAY,-30,getdate()),101)
UPDATE: I still think you need to address the issue you have of TODAY beings stored as some sort of string. If this were a DATE datatype you wouldn't have to deal with this convert nonsense.
Am I missing something here?
DECLARE #Today DATETIME
SELECT #Today = GETDATE() - 30
SELECT #Today AS [Today - 30]
If you don't want the time, don't make it a DateTime data type, instead use DATE:
DECLARE #Today DATE
SELECT #Today = GETDATE() - 30
SELECT #Today AS [Today - 30]
For Oracle try:
TRUNC(SYSDATE) - INTERVAL '30' days
Related
I am trying to implement an SQL query that gets records between today's fixed timing 18:00 and yesterday's fixed timing 18:00 based on a Date time column that I have in my table.
I tried this query
DECLARE #today date = GETDATE()
SELECT *
FROM mytab
WHERE datetimecolumn Between #today-1 + TimeSerial(18,0,0)
And #today + TimeSerial(18,0,0)
But, it's throwing an error Timeserial is not a recognized built-in function name.
Any ideas please?
It would be best to use the DATEADD() function to add the time to the date you want. In this example, it subtracts 6 hours from the first number to get 18:00 yesterday, and then adds 18 hours to get 18:00 today.
DECLARE #today date = GETDATE();
SELECT * FROM mytab WHERE datetimecolumn Between DATEADD(hour,-6,#today) And DATEADD(hour,18,#today);
I would like to differentiate between two dates and round the result to the next day.
For exemple, if I have date1='2020-03-10 11:59:00' and date2='2020-03-10 20:53:00', the difference between date1 and date2 with datediff() is equal to 8 hours. I would like to round this result to have 24 hours.
EDIT
I tried by using dateadd() like this :
select DATEADD(HOUR, DATEDIFF(HOUR, '2020-03-10 11:59:00', '2020-03-10 20:53:00'),0)
Return 1900-01-02 09:00:00.000 doesn't correspond to what I want.
The explanation of the output is not at all clear so I am just guessing here. I am taking the difference in days between two dates. Then adding 1 because if two dates are the same day the difference in days is 0. Then multiplying that result by 24. I changed up the date literal so it is ANSI compliant and will always get the correct date regardless of localization or language settings.
declare #date1 datetime = '20201003 11:59:00'
, #date2 datetime = '20201003 20:53:00'
select datediff(day, #date1, #date2) + 1 * 24
I have a task that I need to show the current date time as ending at 3:00:00 AM at current date. For example, GETDATE() returns the current date time when executes. I need to show it as 9/5/2019 3:00:00 AM instead. Below is my code:
DECLARE #END_SHIFT AS DATETIME
SET #END_SHIFT = '06:00:00 AM'
SELECT
NUMBER_ID,
GETDATE() AS CURRENT_DT,
GETDATE() - #END_SHIFT AS END_SHIFT_DATE
FROM table
My issue when running this is it does not return as ending at 3:00:00AM. Please let me know your direction.
Thanks,
H
A bit of an odd request for sure but you could simply use DATEADD.
SELECT dateadd(hour, 3, convert(datetime, convert(date, getdate())))
If you really need a "hard" time, one option is to use format()
Example
Select format(GetDate(),'yyyy-MM-dd 03:00')
Returns
2019-09-05 03:00
I need to run a SQL script every night that extracts data from the previous 2 days. For example: On July 9 at 1am, the script runs and needs to extract data from July 8 and July 7. On July 10 at 1am, it needs to extract data from July 9 and July 8, etc.
The script is functional, in that it correctly extracts data for a fixed date range (by including the actual date range in the script), but I don't know how to make it do the "2 days prior" part.
Figuring this out is beyond me! Can anyone provide guidance?
Using SQL Server 2014
You can do:
where datecol >= convert(date, dateadd(day, -2, getdate())) and
datecol < convert(date, getdate())
That said, I would be very wary about putting this logic directly into a query. I would create a stored procedure in SQL Server and have it take #fromdate and #todate arguments.
Then, schedule a job that does the above calculation and calls the stored procedure with the right parameters.
One day, when the server is down or the logic fails, you will appreciate having the flexibility to specify the date range yourself.
I would create three variables.
#today: is the current datetime cast to a date to set it to midnight
#startDate: first/start date where I would use the DATEADD function to subtract two days
#endDate: end date that you can subtract 1 second from today
This should get you a date range of 2019-07-07 00:00:00.000 to 2019-07-08 23:59:59.000
DECLARE #today DATETIME = CAST(GETDATE() AS DATE);
DECLARE #startDate DATETIME = DATEADD(DAY, -2, #today);
DECLARE #endDate DATETIME = DATEADD(SECOND, -1, #today);
Time is usually very critical when working with dates, make sure your start date starts at the beginning of the day and your end date ends at the very end of the day!
Your query would then look like:
SELECT *
FROM my_table
WHERE my_date_column BETWEEN #startDate AND #endDate
Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)