select using "current_date" as criteria? - postgresql

If I've been recording data with timestamp. How can I query for today's data? Something similar to this example.
SELECT * FROM table WHERE recorded_time = current_date;
Thanks

DATE_TRUNC('day', recorded_time) = CURRENT_DATE
should do it.

Related

T-SQL apply where clause to function fields not working

I'm having a hard time filtering this view by CreateDate. The CreateDate in the table is in the following format: 2013-10-14 15:53:33.900
I managed to DATEPART the year month and day into separate columns, but now it's not letting me use my WHERE clause on those newly created columns. Specifically, the error is "Invalid Column Name CreateYear" for both lines. What am I doing wrong here guys? Is there a better/easier way to do this than parse out the day, month, and year? It seems overkill. I've spent quite a bit of hours on this to no avail.
SELECT convert(varchar, DATEPART(month,v.CreateDate)) CreateMonth,
convert(varchar, DATEPART(DAY,v.CreateDate)) CreateDay,
convert(varchar, DATEPART(YEAR,v.CreateDate)) CreateYear,
v.CreateDate,
v.customerName
From
vw_Name_SQL_DailyPartsUsage v
full outer join
ABC.serviceteamstechnicians t on v.TechnicianNumber = t.AgentNumber
full outer join
ABC.ServiceTeams s on t.STID = s.STID
where
CreateYear >= '02/01/2018'
and
CreateYear <= '02/20/2018'
You cannot reference an alias from the select in the where
Even if you could why would you expect year to be '02/01/2018'
Why are you converting to varchar
where year(v.CreateDate) = 2018
or
select crdate, cast(crdate as date), year(crdate), month(crdate), day(crdate)
from sysObjects
where cast(crdate as date) <= '2014-2-20'
and cast(crdate as date) >= '2000-2-10'
order by crdate
You could use:
SELECT convert(varchar, DATEPART(month,v.CreateDate)) CreateMonth,
convert(varchar, DATEPART(DAY,v.CreateDate)) CreateDay,
convert(varchar, DATEPART(YEAR,v.CreateDate)) CreateYear,
v.CreateDate,
v.customerName
From vw_Name_SQL_DailyPartsUsage v
full outer join
ABC.serviceteamstechnicians t on v.TechnicianNumber = t.AgentNumber
full outer join
ABC.ServiceTeams s on t.STID = s.STID
where CreateDate BETWEEN '20180102' and '20180220';
More info about the logical query processing is that you cannot refer to a column alias at SELECT in the WHERE clause without using a subquery/CROSS APPLY.

Find most recent date

I have o table name table_1 with 4 columns id, text, fromDate, toDate. The table represents the working experience.I want to create a function which will return the row with columns id, text where the employee worked more recently. This means I need column toDate to be closest to today.
Here is a demonstration of my code:
Select (abs("toDate"-now())) as date_diff
from table_1
Select id,text
from table_1
where (abs("toDate"-now()))=select min(date_diff)
Is this correct or is there something better I can do?
I wil try something like this:
Select id,text
from table_1
where "toDate" = ( select max ("toDate") from table_1 )
It will provide you the latest "toDate" value.
Try this:
select * from table_1
order by to_date desc
limit 1

dynamic query for several date

I wish to make a dynamic query for a set of dates, for example: 06/01/2015, 2015-07-01,2015-05-31, 30.04.2015 ... etc
I can do all these consultation date in one query:
select * from mytable
Where DimtiempoId = 'date'.
I do not want is to make a query for each date. example:
select * from mytable
Where DimtiempoId = "01/06/2015".
select * from mytable
Where DimtiempoId = "01/07/2015".
select * from mytable
Where DimtiempoId = "31/05/2015".
I'm not exactly sure what you're trying to do. Where is your set of dates coming from?
How about:
SELECT *
FROM mytable
WHERE DimtiempoId IN ('2015-01-06', '2015-01-07', '2015-05-31')
If your set of dates is coming from another table, you could do this:
SELECT *
FROM mytable
WHERE DimtiempoId IN (select mydate
from setOfDates)
thanks,
my set of dates are parameters of a sp.
To be more specific I save every result set in a table.
Example:
insert table1
select * from othertbl
where DimtiempoId= "01/06/2015"
insert table2
select * from othertbl
where DimtiempoId= "01/07/2015"
but I want to optimize my sp in a single query.

Get min date in Postgres

How do I get the min date of a timestamp field?
I tried to use
select min(myDatefield) from mytable
but this code doesn't returns the minimum date, but all dates.
Any clue why?
Try this, this might work for you.
SELECT *
FROM
(SELECT MCSSP_AUDIT_ACTIVITY_DATE
FROM MCSSP_MESG_AUDIT_BK
ORDER BY MCSSP_AUDIT_ACTIVITY_DATE ASC
)
WHERE rownum = 1;

Get age from record in table: T-SQL

So each record on my table has a datetime timestamp column.
How do I return the age (in hours), of these records in the database?
Thank you!
select DATEDIFF(hour,timestamp,getdate()) as hours_old
from myTable
datediff(hour,[Your Column], getdate())
http://msdn.microsoft.com/en-us/library/ms189794.aspx
Use the datediff function.
select datediff(hour, getdate(), dob) as age
from ...
Since datediff(hour,'2000-01-01 00:59','2000-01-01 01:00') returns 1, due to the (counterintuitive) way datediff works, you may want something more accurate:
select DATEDIFF(minute,the_timestamp,getdate()) / 60
from TheTable