This question already has answers here:
how to get current/Todays date data in sql server
(5 answers)
Closed 4 years ago.
I need to get all the records using MS SQL SELECT where Modified_Date=Today
my table looks like:
created_by modified_date
20150723081006 2015-07-23 15:14:22.843
20150723081006 2015-09-04 00:14:24.463
20150723081006 2015-09-04 00:14:24.463
One simple way to accomplish this would be to cast your modified date as a date value (removing the time portion) and then comparing it to the current date (again, with time removed):
SELECT *
FROM MyTable
WHERE CAST(Modified_Date AS DATE) = CAST(GETDATE() AS DATE)
Related
This question already has answers here:
Oracle date difference to get number of years
(5 answers)
Closed 2 years ago.
I want to substruct 2 dates(current date and hairdressing_date) to get the result from table to represent data during last 2 years.
I have following SELECT statement:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND (Sysdate - r.hairdressing_date) / 365 < 2;
It is custom, but I am not sure about '/ 365' part of code.
How to get the needed data correctly?
Could you write the correct implementation of this line?
Please use MONTHS_BETWEEN() function like this:
SELECT MONTHS_BETWEEN(TRUNC(SYSDATE), TO_DATE('22.01.2019', 'DD.MM.YYYY'))/12
FROM DUAL;
and revert to this one considering your case:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND MONTHS_BETWEEN(TRUNC(SYSDATE), hairdressing_date)/12 < 2;
This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 years ago.
I need to convert this query from MsSql syntax to Oracle syntax:
select top 1 (convert(varchar, UPDATED_DATE, 23)) as date from DA_CATEGORY order by date desc
How do I do this?
I need the data from both DB types to be the same string / value.
You can use the fetch clause as follows:
select to_char(UPDATED_DATE,'YYYY-MM-DD') as date
from DA_CATEGORY
order by UPDATED_DATE desc
fetch first row only
This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 3 years ago.
select (CURRENT_TIMESTAMP - '60 days'::interval);
This shows me a current time stamp with an interval of 60 days getting subtracted.
SELECT VALUE
FROM schema_name.some_parameter
WHERE some_parameter.NAME LIKE 'some_reference_name'
I want to add the above query in SELECT so that I don't need to add the hardcoded data for 60 days. I want to get it through the parameter variable.
Basically i need to use nested queries where the second query gets me the 60 days value i.e. hardcoded in first query.
Is there a possible solution for my problem?
Maybe something like this?
with v(val)
as
(
VALUES( CURRENT_TIMESTAMP - '60 days'::interval)
)
SELECT v.val from
schema_name.some_parameter cross join v
WHERE some_parameter.NAME LIKE 'some_reference_name'
This question already has answers here:
How can I include null values in a MIN or MAX?
(7 answers)
Closed 4 years ago.
I am working on a SQL query to pull the minimum begin and maximum end date for recurring classes. The way the database is set up, If the classes are not continuing they have an end date, if the classes are continuing then the end date is null. The field is Data Type datetime.
If the end date is null that is essentially the date I want because it is the maximum end date but I cannot seem to work out how to get that to work? I get whatever the last actual date in that field is and cannot get the query to return either null or better yet a default date that I set.
Thanks in advance
If you want the null date as the max, you can do so with a case expression:
select (case when count(*) = count(enddate) then max(enddate) end)
The count(*) = count(enddate) basically says that none of the enddate values are NULL.
If you like playing around with functions, you can also use:
select nullif(max(coalesce(enddate, '9999-01-01')), '9999-01-01')
But I prefer the first method.
This question already has answers here:
TSQL to combine a date field and a time field
(4 answers)
Closed 9 years ago.
I have a database that stores date and time in separate fields. I need to select all records that occurred within + and - 90 minutes of the date and time each of these happened.
I am able to get everything in the format I need to pull it off
SELECT UFV1.USR_DATE
,UFV1.USR_TIME
,LEFT(CAST(DATEADD(minute, -90, UFV1.USR_TIME) AS TIME),8) AS MIN_TIME
,LEFT(CAST(DATEADD(minute, +90, UFV1.USR_TIME) AS TIME),8)AS MAX_TIME
FROM USR_Y_FACILITY_VISIT UFV1
WHERE UFV1.MASTER_CUSTOMER_ID = '2'
ORDER BY UFV1.USR_DATE, UFV1.USR_TIME
Where I am stuck is I need to build a query that takes this info (basically the min/max from each line) then selects all the info in the same table based off that. Thank you for your help I am totally stumped as to where to go next.
Add the key fields for the table to your query as given, along with a corresponding GROUP BY clause. Then query your data table joind to that query as a sun-query on this format:
SELECT *
FROM T
JOIN ( SELECT * ... ) U
ON U.key = T.key
AND T.dateTime BETWEENU.MinDateTime AND U.MaxDateTime
If I correctly understood a question then, this request is necessary for you
SELECT *
FROM USR_Y_FACILITY_VISIT UFV1
WHERE EXISTS (
SELECT 1
FROM USR_Y_FACILITY_VISIT UFV2
WHERE UFV2.MASTER_CUSTOMER_ID = '2'
AND UFV1.USR_DATE = UFV2.USR_DATE
AND UFV1.USR_TIME BETWEEN CAST(DATEADD(minute, -90, UFV2.USR_TIME) AS time)
AND CAST(DATEADD(minute, 90, UFV2.USR_TIME) AS time)
)