oracle date to string - string

I have this problem: I want to convert the sysdate to string, using fillmode on month, day and hour only. However,
select to_char(sysdate, 'fmmm/fmdd/yyyy fmhh12:mi:ss am') from dual
gives me results like
11/13/2013 9:45:**0** am
although it should be
11/13/2013 9:45:**00** am
any thoughts? Thanks in advance

You shouldn't use the FM format model, because FM, as written in the documentation:
FM - Used in combination with other elements to direct the suppression of leading or trailing blanks
So using FM will make your final string shorter, if possible.
You should remove the FM from your format model mask and it will work as you expect:
select to_char(TRUNC(sysdate), 'mm/dd/yyyy hh12:mi:ss am') from dual;
Output:
11/13/2013 12:00:00 am.
I've changed my answer after reading Nicholas Krasnov's comment (thanks).
More about date format models in Oracle Documentation: Format models
Edit
Yes, the code I provided would return, for example, 01-01-2013. If you want to have the month and day without leading zeroes, than you should write it like this: fmDD-MM-YYYY fmHH:MI:SS.
The first fm makes the leading zeroes be truncated. The second fm turns off that feature and you do get leading zeroes for the time part of the date, example:
SELECT TO_CHAR(
TO_DATE('01-01-2013 10:00:00', 'DD-MM-YYYY HH12:MI:SS'),
'fmmm/dd/yyyy fmhh12:mi:ss am')
FROM dual;
Output:
1/1/2013 10:00:00 am.

Related

Create Date from MM/DD format and include current year? Power Query

I have a table that has a series of Columns with data I need to split out. Example below
STATUS#10/16 12:00:00 (CODE)
I've been able to split it easy enough and when I originally tried to set the date on an older dataset it identified it as a date e.g. 16th Oct 2021 However I started to get errors on this date column and trying with different datasets (10/12, 10/13, 10/14) it is not finding the date. I tried the following query code but I'm receiving errors
[STATUS DATE] is split to 10/14, 10/15 etc
#date( Date.Year(DateTime.LocalNow), Date.Month(Text.End([STATUS DATE]), 2), Date.Day(Text.Start([STATUS DATE]),2))
However I'm getting a function error so I tried
Date.From(Date.Day(Text.Start([STATUS DATE]),2) & Date.Month(Text.End([STATUS DATE]),2) & Date.Year(DateTime.LocalNow)
I have also tried to do this from an example column however the query created is looking at the cell value e.g. if 10/14 then 14/10/2021 else if 13/10 then 14/10/2021. This method i feel is prone for error once I include a larger dataset.
Is there anyway I can determine the date value based on mm/dd format? But with year end in mind, make the YYYY be determined by current year unless we move into Jan and then I don't want the Oct, Nov, Dec value showing as 2022.
You don't really show what your original data looks like.
But if it is like:
Source
Then you can use this code in the Add Custom Column dialog:
let
split=Text.SplitAny([STATUS DATE],"#/ "),
mnth = Number.From(split{1}),
dy = Number.From(split{2})
in
#date(Date.Year(DateTime.LocalNow()),mnth,dy)
The Text.SplitAny function lets you input a list of delimiters and the text will split on all of them. So it is relatively simple to extract the month and day values
to create:
Split [STATUS DATE] one more time into [Month] and [Day] column, using the "/" as a separator. Then you don't have to bother with 1 or 2 digit numbers and you can simply use this formula:
#date(Date.Year(DateTime.LocalNow()), [Month], [Day])
DateTime.LocalNow() is a function, so you need to add the brackets.
[Month] and [Day] are numbers already, so you don't need the Date.Month() or Date.Day() functions.

converting a twitter date string into a datetime field in excel

I have a number of excel strings in the format "Mon Nov 25 17:20:47 +0000 2019"
I found an earlier post that recommended using =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) to create a usable date field. However, this drops the time which is an important piece of information.
How can I include the time with the date in order for excel to recognize and sort all the information included in the field?
Thank you in advance!
You can just use the same logic with the date formula, but use TIME instead of DATE and of course extract the correct time into the formula =TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
Edit:
to combine them both in one field, you will need to add them =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) + TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
The rationale of this is because:
Date is expressed in whole numbers i.e. 1 = 01/01/1900, 2 = 02/01/1900, 3 = 03/01/1900... 43794 = 25/11/2019, etc.
Time is expressed as a fraction of the day i.e. 0.5 = 12 hrs/12PM, 0.66666 = 16 hrs/4PM, etc.
so lets say you have 1/1/2019 12.00 PM, the date part that gives 1/1/2019 will be 43466 and the time part will be 0.5. Adding them together will give you 43466.5, and when converted to a date time format it will show as 1/1/2019 12:00 PM.
You can use string functions to create an unambiguous date string, then turn it into a date/time value with a mathematical operation (adding the time value in a string form).
=(MID(A1,9,2)&"-"&MID(A1,5,3)&"-"&RIGHT(A1,4))+MID(A1,12,8)
You'll need to format the result as something appropriate: eg: dd-mmm-yyyy hh:mm

Excel Using concatenate to compare dates

I have a table containing several columns of which one is a date/time field. I am trying to get a count of instances per day using the following
=COUNTIFS(Table4[Data],"Apple",Table4[Date],(CONCATENATE(V4,"*")))
Data Date Comp Date Count
Apple 6/12/18 1:00 PM 6/12/18 12:00 AM 0
Apple 6/12/18 7:00 AM
Orange 6/12/18 1:30 PM
Apple 6/11/18 11:23 AM
From my understanding of all the moving parts here I should be checking to see if "Apple" exists in the data column and then if "6/12/18" with any amount or type of characters after it exists. If both are true I will then get a count + 1 leaving me with a value of 2 in the above example.
What I actually get however is a 0 unless I match the time portion of date the data to be exactly the same and then removed the wildcard ,"*" from the equation.
Am I doing something wrong or can the wildcard not be used to accomplish what I am trying?
Thanks!
I think you should set your criteria properly.
If you add an additional column next to your Date that contains calculates the integer value of your date using INT() and format the display as DATE (m/d/yyyy) you should then be able to use the following COUNTIFS formula
=COUNTIFS(Table4[Data], "Apple", Table4[Date], "=6/12/18")
See the explanatory video from their Office' support site: https://support.office.com/en-us/article/countifs-function-dda3dc6e-f74e-4aee-88bc-aa8c2a866842
If your [Date] column is a datetime or smalldatetime, you can work with it using CONVERT function, depending on how you want to group.
For example, if you don't care about the time to do the group, you could use the next query:
SELECT CONVERT(varchar,[Date],103), [Data], COUNT(*)
FROM [test_delete].[dbo].[Table1]
GROUP BY CONVERT(varchar,[Date],103), [Data]
This should result in something like this:
[Date] [Data] [Count]
11/06/2018 Apple 1
12/06/2018 Apple 2
12/06/2018 Orange 1
Hope this helps you
If your dates are stored as dates instead of text, use the following:
=COUNTIFS(Table4[Data],"Apple",Table4[Date],">="&V4,Table4[Date],"<"&V4+1)

Oracle: Assistance in breaking apart a complex date string

I have a complex date string being read from a csv file. The format is unable to be processed by Oracle's TO_DATE function. Looking for an efficient method to break this string apart and return a date object, to insert into a DATE column. The suggested option of using TO_DATE with 'DD-MON-YY HH.MI.SS AM' does not work. Not variation of this will break up this particular string. Hence the need for a custom function. I have also tried with the 'HH.MI.SS.SSSSS AM' format which also does not work. I have found that if I drop the fractional seconds, it will work. If I run a regex to drop that portion, it should convert as expected.
The string is formatted as: 21-OCT-04 01.03.23.966000 PM
My initial thought is to break up by space first, resulting in three sub strings.
Then break the first substring by - and the second by ., and load the resulting pieces into a DATE object directly.
Is there a better method I could use?
Thank you, Allan
Use what you have, which is a timestamp literal, to create a timestamp, and then cast it "as date":
select
cast(to_timestamp('21-OCT-04 01.03.23.966000 PM', 'dd-MON-rr hh.mi.ss.ff AM') as date) dt
from dual;
DT
----------------------
2004/10/21 01:03:23 PM
(The output format depends on my specific session NLS_DATE_FORMAT, which I actually changed for this illustration to 'yyyy/mm/dd hh:mi:ss AM'.)

Excel function to cut data between to fixed character strings

I have the following text in an excel cell:
SampleID: S-2016-011451 SubmitterID: EIROSSME Sample Name: T1 BTMs - 6/26/16 10:00 PM Lot Nbr: ProductID:
I need to cut the data so that it reads as:
T1 BTMs 6/26/16 22:00
I can format the date using text($cell,"mm/dd/yy hh:mm") but I can't get the =mid(...) to truncate the data between "Name:" and " - ".
1st: =mid(B2;63;26)
2nd: =mid(B5;1;8)
3rd: =mid(B5;11;18)
4th: =concatenate(B7;B8)
If you want to cut between Name: and -, just use:
=find("Name: ";B2)
=find(" -";B2)
and then:
=mid(B2, find("Name: ";B2)+5;find(" -";B2)-find("Name: ";B2)-5)
I.e.:
Assuming you know what to expect after Sample Name:
=MID(A1,SEARCH("Sample Name:",A1)+13,7)
=MID(A1,SEARCH(A4,A1)+LEN(A4)+3,17)
And now you just have to convert the second cell to the date format you want (which you already know how) and concat them like A4&C4 (if C4 is the date after conversion).
Hope it helps ;)
Use SUBSTITUTE to change, so use SUBSTITUTE(a1,"Sample Name:","£££") and SUBSTITUTE(a1,"PM Lot Nbr:","$$$") together, then you'll get £££ T1 BTM......$$$ then you can find the instances of the £££ and $$$ and mid inbetween then, or RIGHT, then LEFT
This gives you the points to cut from and to. You can use the find of the values that we are also substituting, so find PM Lot Nbr etc.
SUBSTITUTE(SUBSTITUTE(J1,"Sample Name","###"),"PM Lot Nbr","|||")
Something similar to this (not complete, I shall finish and tidy)
=MID(SUBSTITUTE(SUBSTITUTE(J1,"Sample Name","###"),"PM Lot Nbr","|||"),LEN("###") + FIND("###",SUBSTITUTE(SUBSTITUTE(J1,"Sample Name","###"),"PM Lot Nbr","|||")),(FIND("|||",SUBSTITUTE(SUBSTITUTE(J1,"Sample Name","###"),"PM Lot Nbr","|||"))-FIND("###",SUBSTITUTE(SUBSTITUTE(J1,"Sample Name","###"),"PM Lot Nbr","|||"))-LEN("|||")))

Resources