Using DAY(), WEEK(), and YEAR() at one query - sql

i using MySQL Query for my task.
And I interested using Date and time function.
can i use DAY(), WEEK(), and YEAR() at one query?
SELECT Object
FROM table
WHERE DAY(date) BETWEEN 1 AND 7
GROUP BY WEEK(date, 1), YEAR(date)
i want do this bcoz i'm worry if sometimes my program have an error because of the date setting and not recognize some date.please give me an input.

Yes, you can use them all in a single query.
The only disadvantage I can think of is that using any of the DAY, WEEK or YEAR functions won't be able to use the index on the column the function is applied to, assuming one is present.
If you're having issues relating to date formatting, you should get familiar with:
DATE_FORMAT
STR_TO_DATE

Related

Bigquery: Getting first day of the week/month in standard SQL

In Bigquery's legacy SQL, I can get the start of week for a date by using
SELECT DATE((UTC_USEC_TO_WEEK(TIMESTAMP_TO_USEC(TIMESTAMP('2017-04-13 20:58:06 UTC')), 0)))
which returns 2017-04-09.
Is there a way to do this in BigQuery's standard SQL? There doesn't seem to be any equivalents for UTC_USEC_TO_WEEK and UTC_USEC_TO_MONTH.
It looks like BigQuery has a function named TIMESTAMP_TRUNC which may do what you want. It is referenced as the replacement for UTC_USEC_TO_DAY(t) in LegacySQL when used with a Day datepart. It also accepts Week and Month as a parameter which may meet your requirements.
TIMESTAMP_TRUNC(TIMESTAMP '2008-12-25 15:30:00', WEEK, 'UTC')
Here is the page for migrating from Legacy to Standard sql
This is better option that works now:
select DATE_TRUNC(date( '2008-12-25 15:30:00'), month)

SQL - subtract between two dates

I'm trying to find the number of days between two date columns.
I tried to use DATEDIFF but I got an error. What more should I do
Thanks,
Are you sure you are using the correct SQL syntax for your database? Since you're using MySQL, you need to do a
SELECT DATEDIFF('2015-06-05', '2015-08-05');
and the difference is always expressed in days.
On SQL Server you need to specify the unit e.g.
SELECT DATEDIFF(day, '2015-06-05', '2015-08-05');

Is this query correct? SELECT s FROM Salesmen s WHERE s.dateOfEmployment < '26-06-2012' ORDER BY s.salepersonId ASC

I am not sure how to query for a date and I have tried it without the ' ' but it does not work. I was wonder if this is correct. Dateofemployment is a Date in the database and the date I put there is the date.
SELECT s
FROM Salesmen s
WHERE s.dateOfEmployment < '26-06-2012'
ORDER BY s.salepersonId ASC
Never assume there's a specific date format (dmy,ymd,mdy,etc.), always use the functions of your DBMS for writing dates instead of writing a string literal, e.g. TO_DATE in Oracle (and some others).
In best case you can use Standard SQL's date literals:
WHERE s.dateOfEmployment < DATE '2012-06-26'
Most DBMSes support it and there's no ambiguity because there's only one format allowed: yyyy-mm-dd
Your query is correct and you must put the two quotes since dateOfEmployment is a date. However, be careful with the position of the day and the month and make sure that your query does what you want.
When you leave out the quotes, you have a simple arithmetic expression: 26 - 06 - 2012. This is equal to something like -1992. That is an integer and not a date, so presumably the comparison will either generate an error or at least never return true.
When putting dates in queries, you should use ISO standard format: YYYY-MM-DD. Most databases will accept the following:
WHERE s.dateOfEmployment < '2012-06-26'
In Oracle, you need to add date before the constant.

Cannot use calculated offset in BigQuery's DATE_ADD function

I'm trying to create a custom query in Tableau to use on Google's BigQuery. The goal is to have an offset parameter in Tableau that changes the offsets used in a date based WHERE clause.
In Tableau it would look like this:
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH") as month_index,
COUNT(DISTINCT user_id, 1000000) as distinct_count
FROM
[Orders]
WHERE
order_date >= DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-12,"MONTH")
AND
order_date < DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH")
However, BigQuery always returns an error:
Error: DATE_ADD 2nd argument must have INT32 type.
When I try the same query in the BigQuery editor using simple arithmetic it fails with the same error.
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),5-3,"MONTH") as month_index,
FROM [Orders]
Any workaround for this? My only option so far is to make multiple offsets in Tableau, it seems.
Thanks for the help!
I acknowledge that this is a hole in functionality of DATE_ADD. It can be fixed, but it will take some time until fix is rolled into production.
Here is a possible workaround. It seems to work if the first argument to DATE_ADD is a string. Then you can truncate the result to a month boundary and convert it from a timestamp to a string.
SELECT
FORMAT_UTC_USEC(UTC_USEC_TO_MONTH(DATE_ADD(CURRENT_DATE(),5-3,"MONTH"))) as month_index;

Select data in date format

I have a query in which I want to select data from a column where the data is a date. The problem is that the data is a mix of text and dates.
This bit of SQL only returns the longest text field:
SELECT MAX(field_value)
Where the date does occur, it is always in the format xx/xx/xxxx
I'm trying to select the most recent date.
I'm using MS SQL.
Can anyone help?
Try this using ISDATE and CONVERT:
SELECT MAX(CONVERT(DateTime, MaybeDate))
FROM (
SELECT MaybeDate
FROM MyTable
WHERE ISDATE(MaybeDate) = 1) T
You could also use MAX(CAST(MaybeDate AS DateTime)). I got in the (maybe bad?) habit of using CONVERT years ago and have stuck with it.
To do this without a conversion error:
select max(case when isdate(col) = 1 then cast(col as date) end) -- or use convert()
from . . .
The SQL statement does not specify the order of operations. So, even including a where clause in a subquery will not guarantee that only dates get converted. In fact, the SQL Server optimizer is "smart" enough to do the conversion when the data is brought in and then do the filtering afterwards.
The only operation that guarantees sequencing of operations is the case statement, and there are even exceptions to that.
Another solution would be using PATINDEX in WHERE clause.
SELECT PATINDEX('[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]', field_value)
Problem with this approach is you really are not sure if something is date (e.g. 99/99/9999 is not date).
And problem with IS_DATE is it depends on configuration (e.g. DATEFORMAT).
So, use an appropriate option.