filter by first 4 digits of a timestamp value in BigQuery standard SQL - sql

I have a variable where the entries are date (timestamp). The exact format is like this: 2009-03-01 00:00:00 UTC.
I want to filter all the rows where year is 2009 (first 4 digits of the timestamp). I am using google BigQuery standard SQL. I tried the following:
WHERE LEFT(CAST(incurred_month_timestamp as STING), '4') LIKE 2013
If anyone can share the query, it would be helpful.
Thanks.

Don't use string functions on dates! BigQuery has lots of useful date/time/timestamp functions.
The one you want is extract():
where extract(year from incurred_month_timestamp) = 2013
Or use a range:
where incurred_month_timestamp >= timestamp('2013-01-01') and
incurred_month_timestamp < timestamp('2014-01-01')

A simpler way is using builtin year function in BigQuery Legacy SQL, which extracts the year from a timestamp.
Here is an example with your code.
where year(incurred_month_timestamp) == 2009

Related

A way to check for specific month in specific year 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)

how to use year function and groupby to do a dynamic search?

Run a query that returns the number of trouble tickets in the VTM024… table, but only the ones created in the year 2015. Note: when setting the where criteria, use a function to extract the Year portion from the timestamp field.
Using the Year function in the first answer, write SQL that shows the number of trouble tickets created in each year dynamically, and do this using a “GROUP BY” clause in the SQL. It should return something like:
2012 10
2013 54
2014 111
etc
I was able to answer first question myself:
SELECT COUNT(CREATE_TIME) FROM TRACS_DW1.VTM024TROUBLE_TKT
WHERE EXTRACT (YEAR FROM CREATE_TIME) = 2015
I need help with 2nd question. I cant get groupby and extract functions together.
i have added a picture of my table.
Add EXTRACT (YEAR FROM CREATE_TIME) as year to your select query and group by the year field.

filtering multiple dates across database in sql?

I have a database that I am trying to filter out by time stamped dates. Since the dates occur over a 15 year time span, I want to filter everything by month (ex all of the january data together). I also have other conditions to filter data, but the logic is getting screwed up when I filter things doing
ex)
where x=y
and id.number=165
and {id.date between UNIX_DATE('01-01-2015') and UNIX_DATE('31-01-2015')
or id.date between UNIX_DATE('01-01-2016') and UNIX_DATE(-31-01-2016')}
I know I can't break it into that braced condition, since it returns an error for me. Is there a way to group the conditions together so the logic is conditionA AND conditionB AND (dateyearA OR dateyearB OR dateyearC or....)?
The ANSI SQL standard provides a way of extracting the month from a date. You should use that:
where x = y and
id.number = 165 and
extract(month from id.date) = 1
That should greatly simplify your code. Not all databases support extract(). Most support either extract() or the month() function. All have this functionality using some function.

BO Universe Designer - Where cluase for a year

I am trying to run a query within Business Objects Universe Designer and I need help with the 'Where' clause.
I want to search for all records that have a 4 digit year (the DB column is in YYYY) less than or equal to 3 years from the current year. So if the year is 2014, I would like to search for every record with a year less than or equal to 2011.
Here is my current where clause:
dbo.DB_TABLE.CATEGORY = 'Actual' and dbo.DB_TABLE.YR <= (convert (SMALLDATETIME, {fn CURDATE()})-3)
Under the 'Date' function, Universe Designer only has: convert (SMALLDATETIME, {fn CURDATE()})
Thanks in advance!!!
Since yr is just a number, you only need to extract the year from the current date:
dbo.DB_TABLE.YR <= datepart(year,{fn curdate()})-3
When writing a SQL statement in the SELECT or WHERE boxes in Designer, you are not limited to only using the functions available in the list box. Any SQL that is valid for the database can be used. The list box is only a helper, and lists commonly-used functions and statements.

SQL search date range in part of text field

I have a text field in a SQL database with dates that are generally like 13 Jan 1897 but could be just 1962 or Jan 2013 or ?1956 etc.
I want to be able to do a WHERE on year ranges. eg. from 1945 to 1987.
I realise that it may be difficult to cater for all formats of the data. I would be happy if it just catered for:
13 Feb 1972, and Feb 1972, and 1972
If the only date formats you are concerned about are like those above and you just want the Year it looks like you could just use the last 4 characters of the value.
In SQL Server that would be:
RIGHT(datefield,4)
Or in most languages (including SQL Server):
SUBSTRING(datefield,LEN(datefield)-3,4)
In ANSI SQL:
MID(datefield,LEN(datefield)-3,4)
Given you are using MySQL. If you want to be able to query your table with proper date comparisons, you may want to create a new field with DATETIME type and fill it with DATETIMES generated from your current field. You'd need queries like :
UPDATE _TABLE_ set NEW_DATETIME_FIELD = STR_TO_DATE(_CURRENT_FIELD_, '%Y') where CHAR_LENGTH(_CURRENT_FIELD_) = 4;
The above query would work for records with values in _CURRENT_FILED_ like '1945'. You'd need to run this query for each type of date string you got in the current field.
The new DATETIME field will allow you to properly use MySQL date functions against it.