SQL to select employees those who have DOB for the day - oracle

I'm new to SQL,
I have field called DOB with 2014-11-07 00:00:00.0 date format in my Database.
I just wanted to select the records which has DOB equal to current date.
I want this to select employees who have DOB for the current day.
Regards
Shridhar

In Oracle you can do this:
select * from employees e where trunc(e.DOB) = trunc(sysdate)

Im assuming this is MySQl or Oracle
If you have a table called Employees, you should be able do use this.
SELECT A.*
FROM EMPLOYEES A
WHERE DATE(A.DOB) = CURDATE();
Here is a resource to learn all about different date functions used in MySQL and Oracle:
https://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_current-date

Since you have tagged MySQL. You can use EXTRACT() function to get the day of the specific date in mysql
select * from employee
where extract(day,DOB) = extract(day, now());

Below query worked fine for me.
SELECT * FROM $A$ WHERE TO_DATE(TO_CHAR(DOB,'MMDD'),'MMDD') =trunc(sysdate)
Regards
Shridhar

Related

Why doesn't this Oracle DATE comparison work

In Oracle 12, if I create a very simple table, TEST_TABLE, with a single varchar2(128) column 'name' and populate that column with lots of strings of '20170831', and my sysdate shows:
SELECT sysdate FROM dual;
29-SEP-17
then why does this SQL query return 0 rows:
SELECT TO_DATE(name,'YYYYMMDD'),
TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY')
FROM TEST_TABLE
WHERE TO_DATE(name,'YYYYMMDD') < TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY');
(This is a very simplified example of a problem I'm facing in my partition maintenance script and have not been able to solve for the last week).
Thank you in advance for any assistance related to the above query.
Midnight(time part is 00:00:00.000):
SELECT TO_DATE(name,'YYYYMMDD'), TRUNC(SYSDATE)
FROM TEST_TABLE
WHERE TO_DATE(name,'YYYYMMDD') <= TRUNC(SYSDATE);
You could also try:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Just don't apply a to_date() to an already date field, this because, it will implicitly convert that date into varchar and then apply the to_date() function to it, for example your query part TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY') is interpreted like this:
TO_DATE(TO_CHAR(TRUNC(SYSDATE)),'DD-MM-YYYY')
TO_CHAR(TRUNC(SYSDATE)) is getting a char something like: '31-AUG-17', and that is not in 'DD-MM-YYYY' format.
And because of that, TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY') gets something like this: 29/09/0017 and your filter goes FALSE and gets no results.

Sybase insert into temp table with identity column

I'm trying to insert records for couple of columns from a physical table into a temp table with customized IDENTITY. It creates the identity column (field name = idnum), but the values are 0 for all rows. I'm using below code. If anyone can help me what I'm doing wrong would be greatly appreciated.
Note: I'm trying this is Sybase ASE 15.7
SELECT
* INTO #achu_test
FROM (SELECT TOP 10
idnum = IDENTITY(8),
First_Name,
Last_Name
FROM Employees) myTable
My bad! I misplaced the IDENTITY. instead of using it before "* INTO", I used inside the Subquery.
SELECT idnum = IDENTITY(8),* INTO #achu_test
FROM (SELECT TOP 10 First_Name, Last_Name FROM Employees) myTable
A good sleep might have given the result for me :)

how to manage Date interval in hive

I'm new in Hive-Hadoop. I have some problem with Date interval management.
In Postgresql, I can get the "6 days" before a given date :
select max(datejour) + INTERVAL '-6 day' as maxdate from table
e.g : if max(datejour) = 2015-08-22 ==> my query returns 2015-08-15
Does somebody can help me on how could I do it in Hive?
thanks.
You can use Hive INTERVAL to achieve this.
select (max(datejour) - INTERVAL '6' DAY) as maxdate from table
Above query should return 2015-08-15
You can find more details -
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types
You can use Hive Date builtin function to achieve this
select date_sub('2015-08-22', 6) from table
Above query should return 2015-08-15
You can find more Hive built-in function here: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
Hope his helps
You can use DATE_SUB function to get your requirement.
Query may look like this(in your case):
select DATE_SUB(from_unixtime(unix_timestamp(cast(MAX(t1.max_date) AS string) ,'yyyy-MM-dd'), 'yyyy-MM-dd'), 6) from (select MAX(datejour) as max_date from table) t1 group by t1.max_date;
Since updating records using UPDATE command is not possible in hive and adding columns through alter command is not recommended as you have to insert values in it through same table.
create external table test(
fields1 string,
field2 string)
create external table test(
fields1 string,
field2 string,
h01 string
)
Insert overwrite table table2
select
fields1,
field2,
case when fields1 = '' then 'OK' else 'KO' end as h01 from table1 where your_condition;

Not getting desired output in Oracle

Table creation query:
create table students(
student_no number,
student_name varchar2(20),
student_addres varchar2(25),
student_dob date
joining_time date
)
Insert query:
insert into students
values (1,'ram','chittoor',to_date('02/04/2012','dd/mm/yyyy'),to_date('01:21:45','hh:mi:ss))
result:1 row inserted
Query to check insert:
select * from students
Result:
student_no student_name student_address student_dob joining_date
.......... ............ ............... ........... ............
1 ram chittoor 2-apr-2012 1-jul-2012
Qhy are the time values not getting inserted properly?
Your date is inserted properly, the tool you're using just seems to show the date without the time potion, check your tool settings;
Oracle has no support for Time only format, only date and time. Here is an excerpt from Oracle type documentation:
In a time-only entry, the date portion defaults to the first day of
the current month
Which is the case, you you get 1-July.
Based on this info, you'll need to rethink your queries.
Here is working example
If you want to get the Time from joining_time column then your,
Select query should be
SELECT student_no,
student_name,
student_addres,
student_dob,
TO_CHAR (joining_time, 'hh:mi:ss') AS joining_time
FROM students

Oracle Date Function with between operator is not giving proper result in oracle 10g?

I developed a query like
select accountname count(transactions)
from table1 group by accountname
where date between '27-mar-2012' and '27-jan-2013'
but it is giving all transactions without considering between and operator i.e it is considering all values from jan to dec. My table contain date column values like 27-mar-2012 ...
If i use
select accountname count(transactions) from table1
group by accountname
where date between cast('27-mar-2012' as date) and cast( '27-jan-2013' as date)
I am getting wrong result.
How can i fix it?
One possibility is that Oracle is running the query:
select accountname, count(transactions)
from table1
group by accountname
Because the where clause is not valid after the group by.
I would suggest:
select accountname, count(transactions)
from table1
where date between to_date('2012-03-27', 'yyyy-mm-dd') and to_date('2013-01-27', 'yyyy-mm-dd')
group by accountname
to_date() is safer than convert(). And it is a good idea (IMHO) to use ANSI standard date formats.

Resources