A way to check for specific month in specific year sql? - sql

Out of curiosity I was playing around with SQL, and as we all know, DATEPART() and DATENAME() can be used to check for tuples with a specific day, year, or month as a condition for us to browse through records in a relation.
But I was just curious whether there is a way to check for both at the same time without using something like:
DATENAME(MONTH,Datevariable1)="February" AND
DATEPART(DAY,Datevariable2)="26"`
to find these records.
Is there a simpler way of doing it? I am asking this question just out of curiosity.

The month() function will call back a specific month. the range is 0 not a month and 1-12 in this case you wanted Feb so we check month against 2. And the day function acts the same way it will look for that day which is set to 26 in this case. Hope it Helps.
The mysql Statement
SELECT *, MONTH(dateColumn)=2 and DAY(dateColumn)=26 as 'MY_MONTH_AND_DAY' FROM yourTable;

By that you want a function that can return "26 Feb" instead of 26 alone and Feb alone?
If that the case you can use something like this in SQL Server
LEFT(CONVERT(VARCHAR(9), dateColumn, 6),6)

Related

SQL - Automatically adjust Where clause to previous month in YYYY-MM format

(This is all steps in containers within an Alteryx flow that is connecting to a Teradata source)
My SQL is incredibly rusty as it's been almost 8 years since I've needed to use it. I know this is a quite basic question. Basically I have several queries that need to be manually adjusted monthly to shift the month. in YYYY-MM format. They look like this:
Is the main one where I just adjust one backwards one month
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = '2023-01'
This is the secondary one where I adjust one backwards one month, and the others are same month or plus a month or more.
and A.B_MONTH in ('2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01','2023-05-01')
and B.Year_month = '2023-01'
How do I adjust the where clause to always be the needed relative references?
Any help is greatly appreciated
I tried using concat but it choked for some reason.
You can try this:
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = DATE_FORMAT( NOW() - INTERVAL 1 MONTH, '%Y/%m')
I don't understand your second need, but you can do it similar to my response.
Just play with the NOW() - INTERVAL X.
Pretty basic stuff. ADD_MONTHS to move your month around, TO_CHAR for your desired format.
To get the previous month:
select to_char(add_months(current_date,-1), 'YYYY-MM')

TERADATA Query for entire month

I wanted to know if there was a way to extract data from a date column pertaining to an entire month.
SEL * FROM DB.TBLNAME TABLE
WHERE TABLE.DATE_ BETWEEN DATE '01-02-2022' AND DATE ' 28-02-2022'
So in the above query(wrote it here as an example so hope i didnt make any typo mistake) im searching for all dates from the 1st to the 28th of february but wanted to know if there was a more elegant method as to select a month match eg. 02-2022.
I hope my question was detailed enough.
Thanks in advance,
For this you could do something like this to make it easier to develop and change as you would need it
SELECT
*
FROM DB.TBLNAME TABLE
WHERE 1=1
AND MONTH(TABLE.DATE) = 2
AND YEAR(TABLE.DATE) = 2022
This is how I would normally do it as you would then be able to write a python script to go with it or a param to dynamically pull the data you need for the month/year

SQL 13 months transaction and quantity data extract broken down per month

I was reading through a couple of older posts and tried to apply the same logic to my question, I need to extract 13 months of data broken down per month, I would also like to apply the data to relevant headers... any suggestions. Please see code below and error received.
SELECT ST.TXDATE, ST.CODE, ST.QUANTITY
FROM StocTran ST
WHERE ST.TXDATE >= DATEADD(MONTH, -13, CAST(GETDATE() AS DATE))
ORDER BY ST.TXDATE
ERROR: [Elevate Software][DBISAM] DBISAM Engine Error # 11949 SQL
parsing error - Expected end of statement but instead found ( in
SELECT SQL statement at line 3, column 27 Error Code: 11949
DATEADD is a function in MS's TransactSQL for Sql Server. I do not know that DBIsam supports it, and it is not listed in DBIsam's list of supported functions here:
https://www.elevatesoft.com/manual?action=viewtopic&id=dbisam4&product=delphi&version=7&topic=functions
Generally, date functions are not portable across different SQL engines, and from that list, one possibility might be to use the EXTRACT function instead:
The EXTRACT function returns a specific value from a date, time, or timestamp value. The syntax is as follows:
EXTRACT(extract_value
FROM column_reference or expression)
EXTRACT(extract_value,
column_reference or expression)
Use EXTRACT to return the year, month, week, day of week, day, hours, minutes, seconds, or milliseconds from a date, time, or timestamp column. EXTRACT returns the value for the specified element as an integer.
The extract_value parameter may contain any one of the specifiers:
YEAR
MONTH
WEEK
DAYOFWEEK
DAYOFYEAR
DAY
HOUR
MINUTE
SECOND
MSECOND
Even if you are in a hurry, I strngly recommend that you study that page carefully.
UPDATE: From googling dbisam dateadd it looks like Elevate don't have a good answer for an equivalent to DATEADD. One of the hits is this thread:
https://www.sqlservercentral.com/Forums/Topic173627-169-1.aspx
which suggested an alternative way to do it using Delphi's built-in date functions (like IncMonth which I suggested you use in an answer to another q. Basically, you would calculate the start- and end-dates of a range of dates, then convert them to strings to construct a WHERE clause with a column date (from your db) which is equal to or greater than the start date and less or equal to the end date.

SQL to select current month MS Access

I'm wondering if it it possible to SELECT records in which [RRRmonth] field is the current month? I've written this SQL in many ways and can't seen to get it to work.
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE (((CFRRR.[workername]) Is Null) AND ((CFRRR.RRRmonth)=Month([RRRmonth])));
Thanks!
The month function will give you the month of a date/time field. The date function will give you the current date. Use the two together will give you the answer you're looking for
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE CFRRR.[workername] Is Null AND month(CFRRR.[RRRmonth])=month(Date());
You also don't need all those parenthesis and makes for a messy query.

Getting the range in SQL when given between

I am wondering if it's possible (without actually parsing the given string) to get the actual range (in terms of days, minutes or seconds) that is specified when you have an SQL statement like
[select 'x'
from dual
where date between to_date('20111113152049')
and to_date('20120113152049')]
I am working on a query where I'm given a string in the form of
"between to_date(A) and to_date(B)"
and would like to get that value in days to compare to a policy we let the user set so they don't enter a date range longer than say a week.
Assuming you're looking for a theoretical answer (that is: don't take this into production) this could work:
Prerequistes:
have three tables: days_seq(day_seq), month_seq(mth_seq) and year_seq(yr_seq)
days has the numbers 1...31, month 1..12, years 2011....?
Use te following query (I used access because I don't have proper RDBMS available here, keep in mind that MS-ACCESS/JET is forgiving in the use of the Dateserial function, that is, it doesn't break when you ask the dateserial for february, 30th, 2012)
SELECT Max(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
-
Min(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])) AS days
FROM days_seq, month_seq, year_seq
WHERE DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])
BETWEEN #2012-02-1# AND #2012-02-28#
The query basically produces a carthesian product of three tables which generates all possible days in months, months in a year for as many years as you have in the years table.
Bonus:
You could off-course generate a permanent Calendar table as X-Zero suggests.
table calendar([date])
INSERT INTO calendar
SELECT DISTINCT DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
FROM days_seq, month_seq, year_seq
You still have to pick your start year and your end year wisely. According to the Maya's an enddate of december 21st, 2012 will do.