Find a date in the past, always based off of todays date - sql

Here is our current string:
select *
from trailer
where trailer.company_id = 'TMS'
and (trailer.is_active = 'A' or trailer.is_active = 'S')
and (trailer.inspection_date >= {d '2019-03-26'})
Problem is the date - I need this to always be 335 days in the past based on todays date - so always >=(todays date -335)
Groovy Console version 2.0.1 - running it on a McLeod LoadMaster version 17.2.0

replace and (trailer.inspection_date >= {d '2019-03-26'}) with DateAdd(yyyy,-1,GetDate())
See if that gives you what you are expecting.

Related

DATE_ADD Function

A glitch in code on my website caused the start date and end date in an sql table to be set to the same day for my online member directory. I have the code glitch corrected, but now need a way to fix the data in the database. I am trying to add a certain number of months (12, 18, 24, etc) to the end_date column depending on the package column. I only want to do this when the start_date column is equal to the end_date column. I have a query started, but keep running into an invalid syntax message. Can someone help? My sql is below
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATE_ADD(months,12,end_date)
WHERE start_date = end_date
AND package_id = 1;
SET end_date = DATE_ADD(start_date, INTERVAL 12 MONTH)
This is for MySQL.
If I understand you correctly, you want to use DATEADD instead.
Your syntax seems like DATEADD (datepart , number , date ) which is not the syntax for DATE_ADD
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATEADD(month, 12 ,end_date)
WHERE start_date = end_date
AND package_id = 1;
Check more here

WHERE clause with 6 months condition

I am working on trying to list every item that hasn't changed in the last month but when I run this query:
select * from AGLT.LTB0040 WHERE wdate> current_date - 6 MONTHS
I get an error message saying that the value is not valid. I have checked the dates listed in the field and they are 20160101. Any idea on where I have gone wrong?
If date column is a representation of a date make a string or numeric comparison.
If the date is a 8 char field
WHERE wdate >= varchar_format(current
timestamp- 6 months,'YYYYMMDD')
If the date is 8,0 field
WHERE wdate >= cast( varchar_format(current
timestamp- 6 months,'YYYYMMDD') as decimal(8,0))

Teradata / Report Buildeer 3.0 cast date between 2 different data types

I want to add a selection criteria to my SQL for a Report Builder report. The user will select a 'from' and 'to' date, e.g., 11/1/2015 and 12/31/2015 with prompts. The sql runs good and brings back everything. I'm trying to narrow the selection to a specific date range.
The date column jsdate in the table is Decimal (8,0). When I look at the dates in this column it looks like 20,150,612, 20,150,609, 20,150,611, etc.
I'm trying to add a selection critieria like
"and cast(jsdate as date format 'mm/dd/yyyy' between 11/1/2015 and 12/31/2015".
I'm sure I have the syntax wrong but I don't know how to correct it. Thanks for your help......
select jsdate, jsn, junit, jmyear, jmake, jmodel, jseries, jbuyid
from vehicle_mgmt.AA0888
where jfinowngp = 'B1' and jfinownbr = '99'
Actually Teradata stores dates using following formula:(year - 1900) * 10000 + month * 100 + day, so try:
select jsdate, jsn, junit, jmyear, jmake, jmodel, jseries, jbuyid
from vehicle_mgmt.AA0888
where jfinowngp = 'B1' and jfinownbr = '99' and
CAST(jsdate - 19000000 AS DATE FORMAT 'mm/dd/yyyy') between
CAST('2015/11/05' AS DATE)
and CAST('2015/12/31' AS DATE)

SQL Get current day in numeric format

I am attempting to grab the current day in the format 01 - 07 or 1 - 7.
This is the query I am using SELECT id FROM tbl_date WHERE weekday = WEEKDAY(NOW) but I seem to get false in return so am not sure I am using the function correctly. The table column weekday contains a number 1 - 7 where 1 is sunday/monday.
Any ideas?
Stupid misstake by me. The correct way would be weekday = WEEKDAY(NOW()) since NOW() itself is a function.
MSSQL can use DATEPART(dw, GETDATE()), MySQL can use DAYOFWEEK()

Concatenating date and time fields

I have a table invoices with this fields:
invDate -> a date field
invTime -> a time field
I need to do querys like
SELECT top 10 * from invoices WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
or something like that. I don't know how to concatenate the invDate and invTime to create a datetime field. The only thing that i could do is this horribly thing:
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate), 17, 52 ) AS MyDatetime
Couldn't even get hour and time with hour(invTime) and minute(invTime):
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate),
HOUR(invTime), MINUTE(invTime) ) AS MyDatetime
I'm doing the querys throught the VFP Odbc Driver via PHP.
You were pretty close. If the value coming from PHP is not of a date/time, how could VFP interpret it properly. VFP also has a function CTOT() (character to time), and expects it in the format of 'yyyy-mm-ddThh:MM:ss??'
yyyy = 4 digit year
mm = 1 OR 2 digit month
dd = 1 OR 2 digit day
T -- literally the letter "T"
hh = 1 OR 2 digit hour (but typical is 2 anyhow)
MM = 1 or 2 digit minute (but typical is 2)
ss = 1 or 2 digit for seconds -- not required
?? = "AM" or "PM" if you wanted to explicitly provide that vs 24 hour clock
The MM and ss are optional, so if you finished with "T1" would be 1:00:00am
Now, to finish your query.
WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
Since this appears to be querying all invoices between a given date/time and NOW (via DateTime()), you don't even need between, you can do
WHERE YourTable.Column > CTOT( '2013-12-17T17:58')
If you specifically DID have a date/time range to consider, THEN you could do something like
WHERE YourTable.Column BETWEEN CTOT( '2013-12-05T10:00') AND CTOT( '2013-12-14T11:58')
PROBLEMS WITH your DATE() and TIME() implementations
The problem is Date() is a function to either return current date, or create based on y/m/d provided such as date( 2013, 12, 7 ). If you are passing a string, use CTOD( 'mm/dd/yyyy' ) such as CTOD( 12, 7, 2013 ).
As for the TIME() function that just expects a number and is of no use for you. From the OleDbProvider, your best bet is to just create a php function that builds a single string in the CTOT() format I've described and pass to the php function the date and time fields. Then use that as your "CTOT( functionReturnResult )"
To add a Date and a Time Field together you will need to convert them both to a same datatype 1st and than just simply add them together something like this....
DECLARE #D DATE = '2013-12-17'
DECLARE #T TIME = '17:58:00'
SELECT CAST(#D AS DATETIME) + CAST(#T AS DATETIME)
Result
2013-12-17 17:58:00.000
Your Query
SELECT top 10 *
from invoices
WHERE CAST(invDate AS DATETIME) + CAST(invTime AS DATETIME)
BETWEEN '20131217 17:58:00.000' AND GETDATE()