Converting time to 24h format - sql

First, I convert a time in the format hhmm to hh:nn (24hr format)
Format(TimeSerial(Left([Table].[TIME],2),Right([TABLE].[TIME],2),0),"hh:nn:ss")
Next, I am trying to combine it with a Date, but the time format changes back to 12h automatically:
TABLE.DATE= Format([TABLE].[DATE] & " " & Format(TimeSerial(Left([Table].[TIME],2),Right([TABLE].[TIME],2),0),"hh:nn:ss"),"mm/dd/yyyy hh:nn");
How can I keep the date in a 24h format within this query?

Always separate value handling from display.
If your table fields are of data type Date, you can simply do:
=Table!Date+Table!Time
The format is for display only.

Related

Access Date Time convert to string. Time is stored as Military, Don't want to convert

I am writing a SQL query for MS Access.
The two fields are DateTime. The start/end time are 22:30/6:30
When I run the following query
SELECT cDate(Format(table1.BeginTime,"hh:mm")),
I get 10:30 PM instead of 22:30. I do not want to convert to 12hrs format. How do I keep the Miliary time format?
So I tried a few things in Access 2013 and it seems that #Bjones has a point.
In SQL Server Format is a clr .net function and format(date, 'hh:mm') will be 12 hour and Format(date, 'HH:mm') will be 24 hour.
But as #Bjones pointed out there may be some question of how your original column is held as text or date or time..... And what I have concluded after trying is that your order of functions is wrong.
cDate(Format(table1.BeginTime,"HH:mm"))
cDate transforms the content to a date but it doesn't format it.
So
FORMAT(cDate(table1.BeginTime), "hh:mm")
or
FORMAT(cDate(table1.BeginTime), "HH:mm")
or
FORMAT(cDate(table1.BeginTime), "Short Time")
should give you what you want if the time is stored as text. If stored as date time you can just drop the cDate all together and stay with
FORMAT(table1.BeginTime, "Short Time")
as #Bjones answered.
You don't tell what the result of the query is intended for.
If you need the date value and to view this in 24 hour format, use:
Select table1.BeginTime
and apply a Format property for the column of h:nn or hh:nn
If you need the date formatted as a string in 24 hour format, use:
Select Format(table1.BeginTime, "hh:nn")
and no Format is needed.
In any case, even though the format string h:mm will work, do use h:nn or hh:nn, as m is for the month.
I think what you need to get rid of is the cDate function as your fields are already in DateTime format.
SELECT Format(table1.BeginTime,"hh:nn")

Combine two fields in Ms Access through queries

have two fields in MS Access, one is a "date/time" datatype and the other a "short text" datatype.
the date/time datatype is a date format: mm/dd/yyyy
the short text datatype is a time 24hr format: 2100
I'm trying to combine the two fields into one without changing their format. So far I've done this:
1) First adding a semicolon for the time:
Format(TimeSerial(Left([Trip_output].[REP_TIME],2),Right([Trip_output].[REP_TIME],2),0),"Short Time")
2) Combining the two fields, date & time:
Format([Trip_output].[REP_DATE],"mm/dd/yyyy")+ ([Trip_output].[REP_TIME],"hh:nn"))
Constraints:
- I need to use only queries to make changes
- I cannot change through design view
What is the best way to combine them to get it to this format: " mm/dd/yyyy hh:nn" ?
You can't just add a date/time date and a string time. You must convert the string time to date/time time first. So, in a query:
REP_DATETIME: [REP_DATE]+TimeValue(Format([REP_TIME, "##\:##"))

Conevrt chararray to Date and add duration in Pig

Iam new to Pig and I have a sample test data of 500 KB which I need to multiply several times to make the file size bigger for some test purpose. The single row in my data is as follows:
( card_description:chararray,
transaction_date:chararray,
merchant_name:chararray,
merchant_city:chararray,
transaction_amount:float
) ;
I want to simply change the transaction_amount and transaction_date for each row several times and then join all the results to make a single big file.
I am stuck in trying to change the transaction_date.
The date value in the file is
27/05/2010 00:00
r1 = FOREACH data GENERATE card_description,ToDate(transaction_date),merchant_name,merchant_city,
ROUND(RANDOM()*5)*transaction_amount;
result =union data,r1;
In order to alter the transaction datei want to use AddDuration function, but in trying to convert chararray to date, I am facing format related issues and unable to understand the solution.
Can someone guide?
After checking out the ways you can invoke ToDate, currently you are invoking ToDate as:
ToDate(milliseconds)
ToDate(iosstring)
And your format is not in milliseconds, nor follows the ISO 8601 format. You should be invoking it like:
ToDate(userstring, format)
Where format is a pattern string that follows these rules.
Therefore, ToDate should be called like:
-- For a 12hr clock
ToDate(transaction_date, "yyyy/MM/dd hh:mm")
-- For a 24hr clock
ToDate(transaction_date, "yyyy/MM/dd HH:mm")
For AddDuration, remember that the second parameter you provide to it must be a string in the ISO 8601 format. Make sure to read the link so you format the string correctly.

Rounding dates to the day in an SQL query

I am stuck with something. I am trying to take a long column of dates of which are formated to show also hours and minutes and run a Group query to paste values at the date level without acknowledging the differences in hours and minutes.. Unfortunately I have no clue how to start. The code i put together so far which returns each grouped date with the hour and minutes is as follows:
st_sql = "INSERT INTO [tblSearchEngine03] ([Date])" & _
"SELECT [tblSearchEngine02].[Date]" & _
"FROM [tblSearchEngine02]" & _
"GROUP BY [tblSearchEngine02].[Date]" & _
"ORDER BY [tblSearchEngine02].[Date]"
Application.DoCmd.RunSQL (st_sql)
Im not sure the best way to truncate the date on table "tblSearchEngine02"..
Focus on the SELECT piece first. You can use DateValue() for your Date field values. Start with this as a new query in the Access query designer:
SELECT DateValue(se02.Date)
FROM tblSearchEngine02 AS se02
GROUP BY se02.Date
ORDER BY se02.Date
Or you could use DISTINCT instead of GROUP BY:
SELECT DISTINCT DateValue(se02.Date)
FROM tblSearchEngine02 AS se02
ORDER BY se02.Date
After you have the SELECT working correctly, you can convert it to an INSERT query (the Access query designer calls it an "append" query).
And when you later build the same statement in your VBA code, include Debug.Print st_sql so that you can view the completed statement text in the Immediate window and make sure it is what you expected. (You can use Ctrl+g to go to the Immediate window.)
One way of doing this is to format the date/time as a date string. If you use YYYY/MM/DD it will sort properly. Otherwise you can convert the date/time to an int to trim off the time and then convert back to a date/time type.
Here is an example of formatting as string:
Format([tblSearchEngine02].[Date], "yyyy/mm/dd")
Here is an exmple of converting to get to a date (the end result will be a date/time data type so it might render as 03/16/2014 00:00 depending on your locale info)
CDate(CInt([tblSearchEngine02].[Date]))
Access stores its dates as floating point numbers where the integer part is the number of days since Jan 1, 1900 and the fractional part is the fraction of the day (time of day). Access is quite happy to treat these dates as numbers without doing any conversions, so:
fix([tblSearchEngine02].[Date])
will trim the fractional part of the day and set the time back to midnight and allow you to group by day.

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.