Ms Access - How to verify the date time format displayed in yyyy/mm/dd HH/MM/SS - sql

May I know could I verify whether the date displayed in the field is in specific format
As per my requirement, the datetime should be displayed in format 'yyyy/mm/dd HH(24hr)/MM/SS'
Eg: The valid value should be '2014/07/18 14:16:48'. If the date is displayed as '18/07/2014 14:16:48', then it is invalid.
Using query how I verify whether it is shown in the format which I have expected. I could use IsDate option to verify it is a valid date and also I could use Mid function to verify the date separator which is '/', but how could I verify the format.
Thanks

If the column is stored as Text, use the SQL Like operator. Select valid dates:
SELECT * FROM myTable WHERE myDate Like "####/##/## ##:##:##"
Select invalid dates
SELECT * FROM myTable WHERE myDate Not Like "####/##/## ##:##:##"
# stands for a single digit.
See Like Operator.
But note that this only makes sense if myDate is a Text! If the type of myDate is Date/Time, the date is stored in a numeric format internally and is only formatted as a date for display. So don't confuse the date value per se and the date as it is displayed.
A Date/Time is always stored in the same way, no matter how it is formatted and displayed!

All you need to do is to open the Table in Design View and to set the Format property.

Related

DB2 Convert Number to Date

For some reason (I have no control over this) dates are stored as Integers in an iSeries AS400 DB2 system that I need to query. E.g. today will be stored as:
20,171,221
Being in the UK I need it to be like the below in Date format:
21/12/2017
This is from my query: (OAORDT = date field)
Select
Date(SUBSTR( CHAR( OAORDT ),7,2) ||'/' || SUBSTR(CHAR ( OAORDT ),5,2) || '/' || SUBSTR(CHAR (OAORDT ),1,4)) AS "Order Date"
from some.table
However, all I get is Nulls. If I remove the Date function, then it does work but its now a string, which I don't want:
Select
SUBSTR( CHAR( OAORDT ),7,2) ||'/' || SUBSTR(CHAR ( OAORDT ),5,2) || '/' || SUBSTR(CHAR (OAORDT ),1,4) AS "Order Date"
from some.table
How do I convert the OAORDT field to Date?
Just to update - I will be querying this from MS SQL Server using an OpenQuery
Thanks.
1) How do I convert the OAORDT field to Date?
Simplest is to use TIMESTAMP_FORMAT :
SELECT DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD'))
2) Being in the UK I need it to be [...] in Date format 21/12/2017 :
SELECT VARCHAR_FORMAT(DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD')),'DD/MM/YYYY')
Note, you didn't specify where you are doing this, but since you tagged as ibm-midrange, I am answering for embedded SQL. If you want JDBC, or ODBC, or interactive SQL, the concept is similar, just the means of achieving it is different.
Make sure SQL is using dates in the correct format, it defaults to *ISO. For you it should be *EUR. In RPG, you can do it this way:
exec sql set option *datfmt = *EUR;
Make sure that set option is the first SQL statement in your program, I generally put it immediately between D and C specs.
Note that this is not an optimal solution for a program. Best practice is to set the RPG and SQL date formats both to *ISO. I like to do that explicitly. RPG date format is set by
ctl-opt DatFmt(*ISO);
SQL date format is set by
exec sql set option *datfmt = *ISO;
Now all internal dates are processed in *ISO format, and have no year range limitation (year can be 0001 - 9999). And you can display or print in any format you please. Likewise, you can receive input in any format you please.
Edit Dates are a unique beast. Not every language, nor OS knows how to handle them. If you are looking for a Date value, the only format you need to specify is the format of the string you are converting to a Date. You don't need to (can't) specify the internal format of the Date field, and the external format of a Date field can be mostly anything you want, and different each time you use it. So when you use TIMESTAMP_FORMAT() as #Stavr00 mentioned:
DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD'))
The format provided is not the format of the Date field, but the format of the data being converted to a Timestamp. Then the Date() function converts the Timestamp value into a Date value. At this point format doesn't matter because regardless of which external format you have specified by *DATFMT, the timestamp is in the internal timestamp format, and the date value is in the internal date format. The next time the format matters is when you present the Date value to a user as a string or number. At that point the format can be set to *ISO, *EUR, *USA, *JIS, *YMD, *MDY, *DMY, or *JUL, and in some cases *LONGJUL and the *Cxxx formats are available.
Since none of variants suited my needs I've came out with my own.
It is as simple as:
select * from yourschema.yourtable where yourdate = int(CURRENT DATE - 1 days) - 19000000;
This days thing is leap year-aware and suits most needs fine.
Same way days can be turned to months or years.
No need for heavy artillery like VARCHAR_FORMAT/TIMESTAMP_FORMAT.
Below worked for me:
select date(substring(trim(DateCharCol), 1, 2)||'/'||substring(trim(DateCharCol), 3, 2)||'/'||'20'||substring(trim(DateCharCol), 5, 2)) from yourTable where TableCol =?;

Convert timestamp to date data type

SELECT to_date(to_char(SYSTIMESTAMP, 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL;
==> 04-MAR-16
Can anybody explain why this select statement doesn't result in '03/04/2016'?
How can I write my selection so that it does result in this, as a date type? I have also tried
SELECT to_date(to_char(trunc(SYSTIMESTAMP), 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL
with the same result.
When a date is returned by a query and displayed, it obviously needs to be formatted in some way. The way a date-type value is formatted is not determined by the query, but by the tool that executes your query and displays the result.
In the case of SQL developer you can set that format as follows:
Choose menu Tools > Preferences.
In the Preferences dialog, select Database > NLS from the left panel.
From the list of NLS parameters, enter "MM/DD/YYYY"
Save and close
See also this question.
Note that to convert a timestamp to date you need just to truncate it: trunc(SYSTIMESTAMP). Converting it to string and then back to a date is unnecessary.
You are converting a datetime to a string and back to a date. Your system defaults the date format to DD-MMM-YY for output purposes; this is the normal default for date in Oracle.
It is important to understand that the internal data structure for date/time types has nothing to do with how they are presented. So, if you want it in another format, convert to a string using to_char().
If you want to change the default format, then look at NLS_DATE_FORMAT. The documentation is here.

selecting data fom a view throws error

I get input as 2011/11/13 00:00:00. So I made the query as:
select * from xxcust_pfoa434p_vw
where week_ending_date = to_date(substr(:value,1,10),'YYYY/MM/DD')
The same statement gives proper result when queried against other tables. But throws error when I query this against the view xxcust_pfoa434p_vw
I have a view xxcust_pfoa434p_vw which has a column week_ending_date of date data type.
The value in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY
Even
select * from xxcust_pfoa434p_vw where week_ending_date='3/2/2014'
also gives
ORA-01843: not a valid month. What is the cause for this error.
You say
"The same statement gives proper result when queried against other
tables. But throws error when I query this against the view
xxcust_pfoa434p_vw"
So clearly the problem is with the view. You also say
"[the view] has a column week_ending_date of date data type. The value
in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY "
Those values would only display like that if the default date mask for you system were MM/DD/YYYY. This is easy enough to check with the query
select * from V$NLS_PARAMETERS
where parameter = 'NLS_DATE_FORMAT';
Personally, my money is on that column not being a date column. ORA-01841 always indicates oracle attempting to cast a string into a date and finding a value which doesn't fit the explicit or default format mask. Plus the so-called date '3/2/2014' lacks leading zeroes and that's suspicious too.
I think whoever wrote that view decided to fix the format of week_ending_date and so deployed TO_CHAR to present a string not a date datatype. A DESC in SQL*Plus or looking at the view TEXT in ALL_VIEWS will reveal the answer.
select * from xxcust_pfoa434p_vw
where week_ending_date=to_date('03/02/2014','MM/DD/YYYY');
Even if you see formatted date in this format - it is only a visual representation, when oracle process your query it automatically convers string given by you into its own interal representation.
It is always better to use proper SQL one YYYY-MM-DD:
for 2nd march: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-03-02', 'YYYY-MM-DD')
for 3rd february: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-02-03', 'YYYY-MM-DD')
this conforms to SQL standard and do not produce confusion between DD/MM/YYYY and MM/DD/YYYY
Just quote from standard:
There is an ordering of the significance of <datetime field>s. This
is, from most significant to least significant: YEAR, MONTH, DAY,
HOUR, MINUTE, and SECOND.
UPDATE: it is very good idea always use to_date function to specify exact format and avoid dependancy on any kind of localization settings

Date not displaying correctly in Oracle

I have a character field that stamps in the order of MMDDYYHHMMSS (note: not a date but character field). I am wanting to kick this out to a date field in my SQL into this format dd.mm.yyyy. hh24:mi.
My problem is that the sql kicks it out to YYYY-MM-DD field without the time. This section of the sql looks like this:
TO_DATE(SUBSTR(MOPACTIVITY.MOPID,3,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,1,2)
||'.'||'20'||SUBSTR(MOPACTIVITY.MOPID,5,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,7,2)
||':'||SUBSTR(MOPACTIVITY.MOPID,9,2)||':'||SUBSTR(MOPACTIVITY.MOPID,11,2)
, 'dd.mm.yyyy. hh24:mi:ss') "XXX",
Any thoughs on how to get the time to convert too?
No need for such a complicated expression:
to_date(MOPID, 'MMDDYYHH24MISS')
will convert the column to a real DATE column assuming the time part is in 24 hour format (00-23, not 00-12). And this will also fail if you don't really have valid dates in the varchar column.
this out to a date field in my SQL into this format
A DATE column does not have "a format"!
The format is only applied when you display it.
In case you mean you want to convert the varchar stored in your column into another varchar that has a different date formatting, the easiest is probably to simply convert the above expression back to a varchar:
to_char(to_date(MOPID, 'MMDDYYHH24MISS'), 'dd.mm.yyyy. hh24:mi')
Before applying something like that, allow me one comment:
Store dates in DATE columns, never ever store them in a VARCHAR column.
If you had done that from the beginning, all you would have to do know is to simply apply a single to_char() to your DATE column to get the display format you want.

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')