SQL search date range in part of text field - sql

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.

Related

Does PostgreSQL have a way to return month names in the local name, not english?

I need to return month names from queries. I think that returning 1 for january, 2 for february and processing that on the front-end is not good. I need to return month names in portuguese Janeiro, Fevereiro and etc. PosgreSQL provides a way to return months in the local format or should I create a table? That's just curiosity, I am completely ok in creating table with dozen of rows manually.
You can use the TM prefix to return the days and months names in the language of locale lc_time:
select to_char(current_date, 'dd TMMonth')
lc_time is inherited from the environment if not otherwise set. You can display its current value with command show lc_time.

filter by first 4 digits of a timestamp value in BigQuery standard 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

where-clause based on current and record date

I'm struggling to add a where-clause based on the difference of the current date and the date of the record entry. There are some other simplistic clauses as well, but I have no problem with those, so I'm making the demo data simple to highlight the issue I'm having.
Demo dataset:
Rec_no Rec_date
77 20170606
69 20170605
55 20170601
33 20170520
29 20170501
Date is recorded in format yyyymmdd and I'd like to build a where clause to only show records that are created X number of days ago from current date - lets say 10.
So, in this case, only records no 33 and 29 should be shown.
Unfortunately I'm not sure on what the actual DB engine is, but it should be something from IBM.
How could this be done?
As suggested in the comments updating the schema to store the date at the correct type is the best option before you start, however if this is not possible for whatever reason, you would first need to convert the stored date to the correct format at runtime.
I'll write an example in t-sql as that's what I know. Once you have worked out your dbms i can edit to the relevant functions/syntax
Select *
FROM Demodataset
WHERE Cast(Rec_Date as datetime) >= dateadd(day,-10,getdate())

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.

Not Getting All Records In a Query

I'm using a GoDaddy-based SQL Server 2005 to store sales from a web site. I have a datePaid column that is formatted as smalldatetime. When I query the database to display all orders for a certain date, using this query:
SELECT DISTINCT
purchase.orderID, wfRegister.firstName, wfRegister.lastname,
storeOrder.subtotal, storeOrder.handling, storeOrder.total,
storeOrder.datePaid, storeOrder.dateOrdered, storeOrder.paypalID
FROM
wfRegister
INNER JOIN
(storeOrder INNER JOIN purchase ON storeOrder.orderID = purchase.orderID) ON wfRegister.customerID = storeOrder.customerID
WHERE
storeOrder.deleted = 0
AND storeOrder.datePaid BETWEEN '03/21/2013' AND '03/21/2013'
ORDER BY
storeOrder
I only get one record returned for the 21st, which is not correct. If I change the query to storeOrder.datePaid BETWEEN '03/21/2013' AND '03/22/2013 I get 7 records returned for the 21st, which is correct, but then I also get records from the 22nd, which I don't want. Is there a way to query the datePaid field for the 21st and get all 7 records returned and not just 1? My client imports the data into Excel and does not want to have to delete the records from the 22nd to get all the records from the 21st
For date arithmetic in SQL Server using strings, I would recommend to always use the ISO-8601 standard format: YYYYMMDD - this is the only format that is independent of any regional and/or language settings.
To reliably get all orders from a given day, you should use
AND storeOrder.datePaid >= '20130321' AND storeOrder.datePaid < '20130322'
The BETWEEN seems handy - but it's inclusive, e.g. you get the data from the 22nd of March, too.
This clause here gives you all records from the 21st of March, regardless of their time - but nothing from the 22nd.