since few weeks I started working on SQL developer.
I noticed an important issue that I do not know how to solve. This is related to the way my SQL developer visualizes the date.
First of all, here are my settings:
and here is my problem:
select to_date('01-01-0001', 'DD-MM-YYYY') from dual;
Instead of correctly visualizin 01-01-2001, it visualizes 03-JAN-0001
select select to_date('01-01-2001', 'DD-MM-YYYY') from dual;
It is interesting to observe that the issue of the day disappear when we change year.
So this -
defines for your session how to display dates and timestamps.
Example:
If you were querying a date w/o supplying a date format, then it adopts said settings.
But if you query a string, and ask Oracle to treat it as a date
select to_date('1-1-1990', 'DD-MM-YYYY') from dual;
The DD-MM-YYYY tells Oracle how to interpret your string as a date.
And then the output is shown using the format defined for NLS
Now, for your scenario, the year '1' - there is a bug in SQL Developer 18.1 that is causing the date to come back as 3 vs 1.
Bug 28093149 - DATE IS RETURNED INCORRECTLY FOR 01/01/0001 - COMES BACK AS 03/01/0001
Related
I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.
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.
I am very new to Cache. I am trying to develop a report with date parameters. When I issue the SQL command:
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) >= convert(sql_date,'08-01-2014' )
I get what I expect two records.
One of which is 10/1/2010 12:00:00 AM.
Then if I issue the command
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) <= convert(sql_date,'08-01-2014' )
I get no values returned?
When I issue the command
SELECT TOP 2 {fn convert('10-03-2010', sql_date) } FROM system.admission_data
I get two NULL values. Clearly I am confused about how Cache works.
I have found that if you use the standard ODBC format (yyyy-MM-dd) for the date you don't need to use the convert and it is much more efficient:
WHERE ad.admission_date <= '2014-08-01'
I formated date incorrectly. I have my code working now. Should look something like select top 2 convert(DATE, '10/03/2010 12:00:00 AM') .... and then I can actually do comparisons.
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.
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?