Below query works fine with the recent dates (for e.g. if the date is 20-JUN-16, 30-SEP-19). But if I query with date value set to 29-MAR-80, 01-JAN-94 it does not return any result. Wondering what could be the reason. Am not allowed to change the existing year format to YYYY.
We are on Oracle 19c.
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = '01-FEB-80';
Here, your date '01-FEB-80' will be converted to '01-FEB-1980', if your two digit year format is RR(in NLS_DATE_FORMAT) and it will be converted to '01-FEB-2080' if your date format is set to YY(in NLS_DATE_FORMAT).
YY -- convert the year to current century year - 80 --> 2080
RR -- convert the year to the year based on current year (1950-2049) so 80 --> 1980.
You can use any of the following two queries to avoid any such dependencies on database settings.
-- use date literal
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = date '1980-02-01';
-- use date string with format
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = to_date('01-FEB-80','dd-mon-rr');
Related
I have a problem with a query on PostgreSQL. I have a case for school where I need calculate the age of a person when he register for school on 1st Septembre of 2005. In a file I have their date of birth and only the YEAR of school registration.
The year registration is a int type ('2005') but I need to convert it to 01-09-2005 (DD-MM-YYYY) which is the start of the school year. How can I do this ?
Should I convert it necessary into a date type if I want to calculate the difference between the date of registration and the date of birth ?
If yes, how can I convert it in order for the date of birth to be on the date type and have a format '01-09-2005'.
I have an answer but that's not the way I should do it :
SELECT AGE('01/09/2005', auditeur.date_nais)
FROM auditeur
JOIN inscription ON auditeur.id_auditeur = inscription.id_auditeur
What I want is to replace the '01/09/2005' with annee ('2005') which is an int type.
That's how you get the desired date from an integer:
(2005 || '-09-01')::date
The concatenation force-converts 2005 to text. Use ISO 8601 format YYYY-MM-DD. Then the cast to date does not depend on the current datestyle setting.
SELECT age((i.annee || '-09-01')::date, a.date_nais) AS age_at_registration
FROM auditeur a
JOIN inscription i USING (id_auditeur)
WHERE a.id = 123; -- select person somehow
Related:
How to concatenate columns in a Postgres SELECT?
Selecting records between two timestamps
There is no need to generate a string to then convert to a date. You have 3 integers: annee from a table and constant values 09 for month and 01 for day. With these just use the make_date function which takes 3 integers and returns a date.
select age( make_date (annee,09,01), auditeur.date_nais)
from auditeur
join inscription
on auditeur.id_auditeur = inscription.id_auditeur
I am trying to get transaction count month to date. For now I am providing date between from_date and to_date.
How can I get results for a particular month in month to date ?
You don't mention what SQL your using, but if its SQL Server then it has a built in function DatePart description I'd expect most of the major SQL dialects to have something similar.
So your where clause would be something like where datepart(mm, to_date) = 4 (for April)
If your version SQL doesn't have the date functions, then you can fallback to the cruder string manipulation. Assuming your date is yyyymmdd, then you could use a sub string function to pull out the mm part, convert that to a integer and perform your comparison.
I have found some fixes about this easily. But I can not change the date column datatype from nvarchar to date in SQL Server. It is a deal for me to select between last 7 days.
How can I do this ?
You must use convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server.
SELECT CONVERT(date,'30-01-2015',103)
First you should be convert and update yyyy-mm-dd your row. After that you can easily get last 7 days.
I had a coworker run a data pull for me. The query was essentially
SELECT a, b, c
FROM table
WHERE date >= 06/01/2018
The where clause being June 1, 2018. The query ran but the date filter was incorrect (not ‘2018-06-01’) How did the server interpret the date used? Was any filtering applied?
If you specified exactly as you have shown it, without quotes, then it would probably have:
1) Calculated 6 divided by 1 divided by 2018 (resulting in an integer zero)
2) Converted the dates in your database to an int to match the compare data type, and done a compare.
I expect this returned all your rows.
You can use this (datediff) function.
SELECT a, b, c
FROM table
WHERE datediff(dateVar, from_unixtime(unix_timestamp('2018/06/01','yyyy/MM/dd'),'yyyy-MM-dd')) >= 0
from_unixtime(,'yyyy-MM-dd') converts string to a string of given format, e.g. '2018-06-01'
Alternatively, these are functions which can help:
date_sub(,xxx) subtracts xxx days from the string, and returns a new
string in the same format.
unix_timestamp(string date,string pattern) converts a
string of a given pattern to unix time stamp, ) if fail.
Reference: How to change date format in hive?
This condition is:
WHERE date >= 06/01/2018
The last part is a numerical expression which I believe is interpreted as (06 / 01) / 2018. Depending on the database, this would either be 0 or about 0.00297, depending on whether your database does integer division.
Now the database has a bit of a conundrum. It has a date on one side and a number on the other. The rules of type conversion say to convert the date to a number. Depending on the database, this could be an error or a valid number -- which would be larger than 0.
The correct way to express this is:
WHERE date >= '2018-06-01'
or:
WHERE date >= DATE '2018-06-01'
I have datetime values in my database table like 05/05/2015 23:00:00. I am putting date filter in my query and try to fetch all the data of 05/05/2015 like this:
select *
from table
where date <= "05/05/2015".
It's not returning the records which have value 05/05/2015 23:00:00 in database.
Please suggest the way..
Using this where clause here
where date <= "05/05/2015"
means: return every row with a date before 05/05/2015 (including the ones with 05/05/2015 00:00:00 - but nothing more).
If you want to get all records for that day, too, you should use
where date < '20150506'
I'd also recommend to use the ISO-8601 date format yyyyMMdd to prevent any regional settings from interfering with your strings representing dates.
And I would also recommend to use something more expressive than just date for your column name - in SQL Server 2008 and newer, DATE is a reserved T-SQL keyword, too - use something like HireDate, SaleDate or something that tells you want kind of date this is