Format Hour in DB2? - db2

Is there any way to display db2 Hour function with AM and PM?
select hour(TIMESTAMP) from ORDERS with ur
This will give out like 5,6,7 etc..
But i want AM/PM after the Hour time.
I'd like to Dislpay as 5AM,6AM,7AM. Is it possible in db2?

Use the TIME() and CHAR() functions:
SELECT CHAR(TIME(timestamp), USA)
FROM Orders
WITH UR
Although, honestly, you should be doing this type of formatting in the application layer, not the SQL Layer.
(Statement run on my local DB2 instance)
EDIT:
Sorry, I missed that part earlier. Going through the documentation has shown me a new function, VARCHAR_FORMAT(). Assuming you're on DB2 9.5, the following should grant you some form of what you're looking for:
SELECT VARCHAR_FORMAT(timestamp, 'HH12 AM')
FROM Orders
WITH UR
Unfortunately, I can't test this myself, as iSeries V6R1 doesn't support the HH12 flag (HH24 only, what?). Otherwise, you're going to have to parse it out yourself.

Related

Truncate datetimes by second for all queries, but keep milliseconds stored in Postgres

I'm trying to find a way to tell Postgres to truncate all datetime columns so that they are displayed and filtered by seconds (ignoring milliseconds).
I'm aware of the
date_trunc('second', my_date_field)
method, but do not want to do that for all datetime fields in every select and where clause that mentions them. Dates in the where clause need to also capture records with the granularity of seconds.
Ideally, I'd avoid stripping milliseconds from the data when it is stored. But then again, maybe this is the best way. I'd really like to avoid that data migration.
I can imagine Postgres having some kind of runtime configuration like this:
SET DATE_TRUNC 'seconds';
similar to how timezones are configured, but of course that doesn't work and I'm unable to find anything else in the docs. Do I need to write my own Postgres extension? Did someone already write this?

Postgres timestamp to date

I am building a map in CartoDB which uses Postgres. I'm simply trying to display my dates as: 10-16-2014 but, haven't been able to because Postgres includes an unneeded timestamp in every date column.
Should I alter the column to remove the timestamp or, is it simply a matter of a (correct) SELECT query? I can SELECT records from a date range no problem with:
SELECT * FROM mytable
WHERE myTableDate >= '2014-01-01' AND myTableDate < '2014-12-31'
However, my dates appear in my CartoDB maps as: 2014-10-16T00:00:00Z and I'm just trying to get the popups on my maps to read: 10-16-2014.
Any help would be appreciated - Thank you!
You are confusing storage with display.
Store a timestamp or date, depending on whethether you need time or not.
If you want formatted output, ask the database for formatted output with to_char, e.g.
SELECT col1, col2, to_char(col3, 'DD-MM-YY'), ... FROM ...;
See the PostgreSQL manual.
There is no way to set a user-specified date output format. Dates are always output in ISO format. If PostgreSQL let you specify other formats without changing the SQL query text it'd really confuse client drivers and applications that expect the date format the protocol specifies and get something entirely different.
You have two basic options.
1 Change the column from a timestamp to a date column.
2 Cast to date in your SQL query (i.e. mytimestamp::date works).
In general if this is a presentation issue, I don't usually think that is a good reason to muck around with the database structure. That's better handled by client-side processing or casting in an SQL query. On the other hand if the issue is a semantic one, then you may want to revisit your database structure.

FileNet - SQL for a date property between specific hours

I would like to fetch documents that a property date hour is between midnight and 4 AM.
I tried this:
SELECT [This], [Date], FROM Folder_Type_1
WHERE DATEPART(hh,[Date]) >= 0
AND DATEPART(hh,[Date]) <= 4
ORDER BY Date
and
SELECT [This], [Date], FROM Folder_Type_1
WHERE CONVERT(VARCHAR(8),Date,108) between '00:00:00' and '04:00:00'
ORDER BY Date
But none of them is working when I test it in the SQL query builder in the FEM.
DATEPART and CONVERT are not recognised. What is the correct way to do it?
I didn't find anything interesting in this SQL syntax reference.
Thank you in advance!
You are trying to use T-SQL functions within Content Engine Query Language. While its syntax might look like SQL, it is actually not. Not to mention it is obviously not T-SQL.
As of today, it is not possible to accomplish what you want. TimeSpan function introduced in the version 5.1 allows some manipulations with date parts. Those, however, are not sufficient for your task. You might want to check TimeSpan documentation.
I have used the follwoing before:
where c.DateCreated >= 20130101T000000Z
This is a snippet from a query executed using the api an not the fem, but in principle this should be the same sql

Calculating days between last login and current date

Upon logging into their accounts, each user has their login date and time stored to the database. What I was looking to do however is figure out the amount of days (or preferably convert into months if greater than a month) so that if a user views their profile they can see how active the band are. Also, this could benefit me in terms of keeping active profiles top of the agenda for content on the site so that it doesn't become stale from inactive users content filling up main page content.
I'm using ColdFusion so i'd be looking for a way to find for example how many days ago #lastLogin# was from #now()#. So say if the date of the last login was 23/04/2013 and todays date is 29/04/2013 it would read "Last Active, 1 day ago." However if the last login was 23/03/2013, it would read "Last Active, 1 month ago".
Anybody know how to do this? Thanks.
P.S I currently have no code from testing this as I have no idea where to start in terms of achieving this.
Use DateDiff
<cfset days = dateDiff("d", LoginDateVariable, now()) />
It's as simple as that.
P.S I currently have no code from testing this as I have no idea where
to start in terms of achieving this.
This doesn't answer your direct question but to help you know where to get started, I would strongly suggest reviewing the built in ColdFusion functions and tags that are available to you.
Tags
Tags by function
Functions
Functions by category
Also, Google searches usually land you at the docs, just add "coldfusion" to your search string. Searching google for coldfusion date functions yields very helpful answers, the first of which are a list of all ColdFusion date functions.
Dale's answer is spot on. But I would also suggest returning it as a variable with your query. Let the SQL server do the work. It's very efficient for those types of calculations. Not that CF can't do them well, too. But it's probably more appropriate for SQL to do that lifting. Especially if you're already returning the lastLogin date.
It would be similar to the CF solution:
SELECT ...., lastLogin, DATEDIFF(d, lastLogin, GETDATE()) AS LastLoginDays
FROM ....
WHERE ....
That would give you the number of days. You'd have to decide how you wanted to define a month if you wanted to break it out by month/day. That would get a bit more complex. You could write a SQL function that could be run on both dates and give you an accurate count of days/months/years since last login.
One other thing to keep in mind: Where are the dates being generated? When you insert loginDate into the database, are you doing a now() in CF before you insert it or are you doing a getDate() in SQL when you insert it? Again, I would let the database do your date logic, but you'd want to compare the two dates from the same source. For instance, if your loginDate was a database getDate() then you may not want to compare that to a CF now(). One goes by the datetime of the SQL server and the other goes by the datetime of the CF server. They could be different.

T-Sql date format for seconds since last epoch / formatting for sqlite input

I'm guessing it needs to be something like:
CONVERT(CHAR(24), lastModified, 101)
However I'm not sure of the right value for the third parameter.
Thanks!
Well I'm trying to write a script to copy my sql server db to a sqlite file, which gets downloaded to an air app, which then syncs the data to another sqlite file. I'm having a ton of trouble with dates. If I select a date in air and try to insert it, it fails because it's not in the right format... even if it was a valid date to begin with. I figured I'd try to experiment with the unix time since that's the only thing thats worked so far. I am considering just leaving them as varchar because I don't sort by them anyway.
Last epoch is when 1970 GMT?
SELECT DATEDIFF(s,'19700101 05:00:00:000',lastModified)
See also Epoch Date
sqlite> select datetime();
2011-01-27 19:32:57
sqlite> select strftime('%Y-%m-%d %H:%M:%S','now');
2011-01-27 19:33:57
REFERENCE:
(Date time Functions)[http://sqlite.org/lang_datefunc.html]
I wound up using format 120 in MS SQL:
convert(char(24), lastModified, 120)
Each time I needed to a select a date in SQLite for non-display purposes I used:
strftime(\"%Y-%m-%d %H:%M:%S\", dateModified) as dateModified
Now I just need a readable/friendly way to display the date to the user!
edit: accept answer goes to whoever shows me how to display the date nicely from sqlite ;p
Define "last epoch". Does this come close?
Select Cast(lastModified As Integer)
If you store them as varchar, store them as YYYYMMDD. That way you CAN sort by them later if you want to.
SQL server has only 2 failsafe date formats
ISO = YYYYMMDD, run this to see that
select convert(varchar(10),getdate(),112)
ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(no spaces) run this to see that
select convert(varchar(30),getdate(),126)
To learn more about how dates are stored in SQL server I wrote How Are Dates Stored In SQL Server?