between date cakephp 3 - date

when I make a between a date and see the log this adds spaces
$campaigns = TableRegistry::get('Administrator.Campaigns');
$campaignsActual= $campaigns->find('All')
->where('parent_id IS NULL AND '.date("Y-m-d").' BETWEEN start AND end')
but
SELECT
Campaigns.id ASCampaigns__id,
Campaigns.name ASCampaigns__name,
Campaigns.description ASCampaigns__description,
Campaigns.start ASCampaigns__start,
Campaigns.end ASCampaigns__end,
Campaigns.status ASCampaigns__status,
Campaigns.parent_id ASCampaigns__parent_id,
Campaigns.lft ASCampaigns__lft,
Campaigns.rght ASCampaigns__rght,
Campaigns.created ASCampaigns__created,
Campaigns.modified ASCampaigns__modified
FROM
campaigns Campaigns
WHERE
parent_id IS NULL
AND 2017 - 01 - 23 BETWEEN start
AND end
The date 2017 - 01 - 23 generates spaces How to ensure that these spaces are not converted?

The problem is ->where('parent_id IS NULL AND "'.date("Y-m-d").'" BETWEEN start AND end')
"'.date("Y-m-d").'"

Related

How to return null if an error for try_parse

I am trying to simply return NULL if I get an error from TRY_PARSE. I am using TRY_PARSE to parse out a datetime from a string.
create table #example (
ID int identity(1, 1)
, extractedDateTime varchar(50)
)
insert into #example (extractedDateTime)
values ('7/19/21 11:15')
,('/30/21 1100')
,('05/15/2021 17:00')
,('05/03/2021 0930')
,('5/26/21 09:30')
,('05/26/2021 0930')
,('06/09/2021 12:00')
,('07/06/2021 13:00')
,('6/15/21 12:00')
,('07/09/2021 07:30')
,('07/14/2021 13:20')
,('/19/2021 10:30')
,('7/22/21 1030')
,('7/21/201')
,('06/21/21 11:00')
select exm.ID, exm.extractedDateTime, [TRY_PARSED] = TRY_PARSE(exm.extractedDateTime as datetime2 using 'en-US')
from #example as exm
drop table #example
In the above example there is ID 14: '7/21/201' which will be parsed as from the year 201 (presumably it was meant to be 21 or 2021). I have gotten this to parse as datetime2, originally I was using datetime. I am inclined to still use datetime, but what I would like is to return NULL for that particular row. Instead a get a lengthy error message about a SqlDateTime overflow, because of using datetime of course.
The reason I want to go back to using datetime is this is an incorrect value, and using datetime might help filter out erroneous values like this. Also, I'd like the query to be able to return NULL anyways, whenever this little bit encounters an error so that it doesn't stop the entire query (this is part of a much larger query).
How can I return NULL for this record? Any advice would be greatly appreciated!
UPDATE:
Here is the picture I get from executing the seen SELECT statement:
As others have noted, the TRY_PARSE is functioning as expected and documented.
That doesn't help your case, though, so I'd suggest setting an arbitrary minimum date that any date prior to that gets assigned a NULL.
Sample code:
SELECT exm.ID,
exm.extractedDateTime,
[TRY_PARSED] = CASE
WHEN TRY_PARSE(exm.extractedDateTime AS DATETIME2 USING 'en-US') < '1970-01-01'
THEN NULL
ELSE TRY_PARSE(exm.extractedDateTime AS DATETIME2 USING 'en-US')
END
FROM #example AS exm;
Results:
1
7/19/21
11:15
2021-07-19 11:15:00.0000000
2
/30/21 1100
NULL
3
05/15/2021
17:00
2021-05-15 17:00:00.0000000
4
05/03/2021
0930
NULL
5
5/26/21
09:30
2021-05-26 09:30:00.0000000
6
05/26/2021
0930
NULL
7
06/09/2021
12:00
2021-06-09 12:00:00.0000000
8
07/06/2021
13:00
2021-07-06 13:00:00.0000000
9
6/15/21 12:00
2021-06-15 12:00:00.0000000
10
07/09/2021
07:30
2021-07-09 07:30:00.0000000
11
07/14/2021 13:20
2021-07-14 13:20:00.0000000
12
/19/2021 10:30
NULL
13
7/22/21 1030
NULL
14
7/21/201
NULL
15
06/21/21
11:00
2021-06-21 11:00:00.0000000
The advice is not to do anything since what you ask for is the default behavior of TRY_PARSE. Check the documentation
https://learn.microsoft.com/en-us/sql/t-sql/functions/try-parse-transact-sql?view=sql-server-ver15

how to change the timestamp format in other language PostgreSQL

PostgreSQL timestamp:
begin= '2018-06-07 08:29:49'
end = '2018-06-10 04:07:57'
QUERY:
select sa.\"end\"::timestamp(0) - sa.begin::timestamp(0)
from tabelle sa;
Output:
2 days 19:38:08
I would like to change this output "days" into "Tage", like:
2 Tage 19:38:08
Most important, when there is month different then it should display months in 'Monate', the same goes for years to 'Jahre'.
i tried to look about dateformat, but i need to change expr format not format like '%Y %m %d ...etc.
i tried this too:
to_char(extract(year from (sa."end"::timestamp - sa.begin::timestamp)),'00')||' Jahre '||to_char(extract(month from (sa."end"::timestamp - sa.begin::timestamp)),'00')||' Monate '||to_char(extract(day from (sa."end"::timestamp - sa.begin::timestamp)),'00')||' Tage '||to_char(extract(hour from (sa."end"::timestamp - sa.begin::timestamp)),'00')||':'||to_char(extract(minute from (sa."end"::timestamp - sa.begin::timestamp)),'00')||':'||to_char(extract(second from (sa."end"::timestamp - sa.begin::timestamp)),'00')
Output:
00 Jahre 00 Monate 02 Tage 19: 38: 08
it doesn't look good, cause month or year shall appear if there is number.
Important, only this 'day' needed to be changed in 'Tage' not setting character UFT8 or other things complete in german language!!!
Please i need your support.
There is some solution but in other way:
select extract(day from (sa.\"end\"::timestamp - sa.begin::timestamp))||' Tage '||
to_char(extract(hour from (sa.\"end\"::timestamp-sa.begin::timestamp)),'00')||':'||
to_char(extract(minute from (sa.\"end\"::timestamp - sa.begin::timestamp)),'00')||':'||
to_char(extract(second from (sa.\"end\"::timestamp - sa.begin::timestamp)),'00')
from tabelle sa
Output:
2 Tage 19: 38: 08

Producing date from year and month values in PostgreSQL

Hello I'm having two problems with converting a concatenated date value into an actual date.
I've tired looking here to convert the concatenated value with to_char(DATE ...) but I keep getting odd dates. I think it is because my month does not have a zero padding in front of it.
This is my base query:
SELECT
expiry_month,
expiry_year,
to_date(CONCAT(expiry_year, expiry_month), 'YYYY/MM'),
FROM thisTable
Here is an example of the data output:
expiry_month expiry_year concatvalues
9 2018 20189-01-01
1 2019 20191-01-01
5 2016 20165-01-01
3 2019 20193-01-01
10 2017 201710-01-01
2 2020 20202-01-01
I think I need to LPAD() my month value to get the correct date parsed. E.g. 01 not 1, and 05 not 5.
However when I try to LPAD the month values it does not work. I've tried:
lpad(to_char(expiry_month),2,'0'),
I get this error 'HINT: No function matches the given name and argument types. You might need to add explicit type casts.'
Which I don't understand because lpad is a function. Any suggestion on how to use LPAD()?
Thank you for the advice.
EDIT 1
I've tried to update the to_date() function with this code:
to_date(CONCAT(payment_cards.expiry_year || ' - ' || payment_cards.expiry_month || ' - 01'), 'YYYY-MM-01') and now it is throwing a different error:
ERROR: invalid value "- " for "MM" DETAIL: Value must be an integer.
I'm still thinking I need to pad the month date?
There's a '/' missing:
SELECT
expiry_month,
expiry_year,
to_date(CONCAT(expiry_year, '/', expiry_month), 'YYYY/MM') AS the_start_of_year_month
FROM thisTable ;
will produce:
expiry_month | expiry_year | the_start_of_year_month
-----------: | ----------: | :----------------------
9 | 2018 | 2018-09-01
1 | 2019 | 2019-01-01
5 | 2016 | 2016-05-01
3 | 2019 | 2019-03-01
10 | 2017 | 2017-10-01
2 | 2020 | 2020-02-01
The date format is specifying '/' and it wasn't there, so, the whole text was taken as the year, and the month and day were taken as 1/1. CONCAT('2018','9') was just returning '20189' (which is a valid year).
dbfiddle here
Use:
make_date(year int, month int, day int)
like:
make_date(expiry_year, expiry_month, 1)
Postgresql documentation

Dateparse MON in Tableau

I have a data source that returns a date as a string in the form of 'MON YYYY' (APR 2014, MAY 2014, etc.).
I tried making a calculated field off of this information with the following formula:
DATEPARSE('MMM YYYY', [Field1])
This is a sample set of the data I'm getting (I added the pipe as a divider):
Field1 || Calculated Field
APR 2014 || 12/22/2013
APR 2015 || 12/28/2014
APR 2016 || 12/27/2015
AUG 2014 || 12/22/2013
AUG 2015 || 12/28/2014
AUG 2016 || 12/27/2015
I've also tried to add a day field, but that results in the same incorrect data as above:
DATE(DATEPARSE('dd MMM YYYY','01 ' +[Field1]))
Is there something I'm perhaps misunderstanding about the dateparse function?
It turns out that YYYY means something totally different than yyyy. The capitalized MMMwas necessary for the MON type description. This worked for me:
DATE(DATEPARSE('MMM yyyy',[Field1]))
If you date the date off you'll get the hour, minute, second fields as well.
Dateparse converted it from a string [Field1] into a Date type using the aforementioned format of three digit month, a space, and a four digit year (e.g. AUG 2014 -> 8/2/2014).

Casting a string to Date and Time in PostgreSQL

I have a table in GreenPlum (PostgreSQL) with all fields as sting, and I want to edit the types :
To do this I created a view :
CREATE VIEW typed_view AS
SELECT CAST(sid AS bigint), CAST(gid AS bigint),
...
But I have a problem with the Date and Time fields, I tried this command but it didn't work :
to_utc_timestamp(from_unixtime(unix_timestamp(eventdatetime,"yyyy-MM-dd
HH:mm:ss")),'UTC') AS eventdatetime,
After that I tried the PostgreSQL notation :
to_timestamp(eventdatetime, 'YYYY Mon DD HH24 MI SS') AS eventdatetime,
But still not working.
Anyone knows how to convert it ?
I also have this command that is not working :
CASE WHEN fix = "True" THEN TRUE ELSE FALSE END AS fix,
Thanks in advance
You didn't provide example data so I'm going to assume your data looks like "YYYY Mon DD HH24 MI SS". So January 4, 2016 at 2:15:20 PM would look like '2016 Jan 04 14 15 20' in your data. So with this example data, the conversion would look like this:
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss') as col1;
col1
------------------------
2016-01-04 14:15:20-05
(1 row)
Now this is a timestamp which also include the timezone offset which for my server is -5. To convert this to a timestamp without the timezone, you just add ::timestamptz.
gpadmin=# select to_timestamp('2016 Jan 04 14 15 20', 'yyyy mon dd hh24 mi ss')::timestamp as col1;
col1
---------------------
2016-01-04 14:15:20
(1 row)
A very important note on this. It is costly to convert data from a string to a different datatype. That is the same in all databases too. It is better to incur the expense of this conversion once rather than doing it for every SELECT statement. So, I also suggest you materialize this transformation into a physical table rather than using a VIEW.