Date in sql database - sql

i want to access date from my table and i only want only particular dates to display (for eg: date=1) .
Is there any way to access date,month and year individually from date format?
please help

Avinash is this what you are looking for ?
select MONTH('10/15/1981') --10
select DAY('10/15/1981') --15
select YEAR('10/15/1981') --1981
Select Dates
from tablename
Where MONTH(Dates) = 10

Related

How to calculate difference of dates in different formats in Snowflake?

I am merging 2 huge tables in Snowflake and I have 2 columns (one on each table):
"Year_birth" and "Exam_date" and the info inside looks like this respectively:
"1918" and "2007-03-13" (NUMBER(38,0) and VARCHAR(256))
I only want to merge the rows where the difference (i.e., age when the exam was made) is ">18" and "<60"
I was playing around with SELECT DATEDIFF(year,Exam_date, Year_birth) with no success.
Any ideas on how would I do it in Snowflake?
Cheers!
You only have a year, so there is not much you can do about the specific day of the year -- you need to deal with approximations.
So, extract the year from the date string (arggh! it should really be a date) and just compare them:
where (left(datestr, 4)::int - yearnum) not between 18 and 60
I would strongly advise you to fix the database and store these values using a proper date datatype.
You will need to convert the integer year into a date before doing a datediff
example:
set YearOfBirth = 1918;
set ExamDate = '2007-03-03'::DATE;
-- select $YearofBirth as YearofBirth, $ExamDate as ExamDate;
select $YearofBirth as YearofBirth,($YearofBirth::TEXT||'-01-01')::DATE as YearofBirthDate, $ExamDate as ExamDate, datediff(year,($YearofBirth::TEXT||'-01-01')::DATE,$ExamDate) as YearsSinceExam;
USE YEARS_DIFF IN WHERE CLAUSE TO FILTER DIFFERENCE BETWEEN 18 & 60
SELECT DATEDIFF( YEAR,'2007-03-03',TO_DATE(2018::CHAR(4),'YYYY')) YEARS_DIFF;

select records from a table for each different date in single iteration of SQL query

Sorry for the vague Problem statement, maybe I could not frame it the right way hence couldn't get much help from the internet.
My basic intent is to select two columns from a table, where the table has been partitioned based on dates, and has huge number of records.
The intent is to select records for each date from jan 1 to nov 30, can't do it using date between statement, I need to input each date separately in the iteration in for loop.
Can anyone help writing a dummy sql query on this?
Thanks,
you can use any scripting language to iterate over each day to achieve.
below is a sample (untested code)
import pyodbc
conn = pyodbc.connect(DSN='dsnname',autocommit=True,ansi=True)
cur = conn.cursor()
sql = "INSERT INTO tgt_table SELECT col1,col2 FROM src_table WHERE partitioned_date_column = date '2014-01-01' + INTERVAL '%d' DAY"
for i in range(1,30):
cur.execute(sql % i)
Assuming you use oracle, this dummy select should give you the list of dates to join
select to_date('01.01.2014','dd.mm.yyyy') + level - 1
from dual
connect by level -1 <= to_date('30.11.2014','dd.mm.yyyy') - to_date('01.01.2014','dd.mm.yyyy')
I dont know how your tables look like, but have you tryed something like this:
"WHERE {yourRow} > X GROUP BY date"
MySQL Query GROUP BY day / month / year
Hope this can help you :-)
SELECT col1, col2 FROM {table} WHERE datecol between '2000-01-01'
and
DATE_ADD('2000-01-31',INTERVAL 1 DAY)
OR
SELECT col1, col2, FROM {table} GROUP BY DAY(datecol)

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause

Microsoft Access SQL to query a date range ignoring the year

I need to select records based on a range of month/day values, disregarding the year. I am querying an Access database, and I found a query here but it seems to select a range of years.
SELECT *
FROM Factory
WHERE YEAR(date) BETWEEN 1998 AND 1999
I have tried this query, but it only shows birthdays in the current year :
SELECT * FROM user where birthday Between #09/05# and #10/10#;
In my database the birthday column contains the year to count how old they are. What query can I use to perform what I want?
Try
SELECT * FROM user
WHERE (month(birthday) * 100) + day(birthday) between 0131 and 1231
Try this.
SELECT *
FROM user
WHERE birthday between #1991/12/31# and #1992/01/31#;
OR
SELECT *
FROM user
WHERE birthday between #12/31/1991# and #01/31/1992#;
OR
SELECT * FROM
user
WHERE birthday between #1991/12/31# and #1992/01/31#

How to get previous months data using KDB query?

How can I retrieve data from the previous month in such a way that, if the query were to be automated, the date value in the query would change accordingly, every month?
So for example:
When query is run on 2012.01.01 --> select * from Table where date >= 2011.12.01
When query is run on 2012.02.01 --> select * from Table where date >= 2012.01.01
When query is run on 2012.03.01 --> select * from Table where date >= 2012.02.01
and so on..
Help would be much appreciated!
I assume you are using Oracle Database;
select * from Table where date >= ADD_MONTHS(TRUNC(SYSDATE),-1)