I have a DB with date looks like BIGINT "15321600".
It's represent 17/2/2017
How can I convert it?
edit:
I understood that the number represent the minute passed from 1/1/1988...
You really need logic to specify what the conversion is. My trial-and-error, it looks like minutes since 1988-01-01 produces your value.
That means that you can use logic like this
select date '1988-01-01' + col * interval '1' minute
Of course, date/time functions vary by database, so the exact syntax might be different in your database.
Related
can anyone explain me the working of trunc(date) function in oracle.
my query is as below.
select trunc(tran_date) from tablename;
i have not passed any format type.
If i compare the date present in table without having trunc(date) it will not give any output.
and if compare date table with trunc(date) it will give me proper ouptut.
please explain how it is working.
and is there any replacement for trunc function as it is taking too much time.
trunc(tran_date) returns the date portion of the date column with no time component (which is midnight at the start of the day).
Despite its name, the date data type in Oracle includes the time. This is even more confusing because you sometimes do not see the time in the result set (depending on how you access the data).
The dates that you are comparing to have no time component. So, the comparison works with trunc(). But the time component on tran_date prevents the comparison from working without trunc().
I can't understand how to convert this type of real - 42389.520752314813 to timestamp.
I got this data from one source, but I need to convert it to normal timestamp format.
I think you have received wrong input data.
this type of timestamp is only occurring when Destination tool is excel and which has CELL as "Number" type, and during Copy-Paste of timestamp, destination field has Calculated it as Mathematical function. Please re-verify your source. I am sure about this mathematical calculation. please check the below sample of such data. So practically you cannot perform its reverse operation.
Hard to do without knowing what timestamp should be returned.
If the date is sourced from a SQL Server it might be '2016-01-22 12:29:53':
cast( date '1900-01-01' + myCol as timestamp(3))
+ (cast(86400 * (myCol mod 1) as dec(12,6)) * interval '00:00:01.000000' hour to second)
If it's from Excel it's two days earlier and you must start at '1899-12-30'
I have a star schema database with an Hour-Dimension (Time-dimension), with the following columns in it:
ID, ON_HOUR, ON_DAY, IN_MONTH, IN_YEAR
I then query the database, and I want to find all entries within an interval of given dates, based on this Hour-Dimension.
However comparing the ON_DAY attribute with the interval days and so on with IN_MONTH and IN_YEAR, I can often reach a case where I receive no data, if the interval spans over several months. Thus I need to convert these values to a timestamp, however I am querying into the database, so how do I compare my given timestamps with the time data properly? I do not have a stored DATE nor TIMESTAMP in the database - should I change this?
Right now, my best bet is something like this:
to_timestamp('H.IN_YEAR-H.IN_MONTH-H.ON_DAY H.ON_HOUR:00:00', 'YYYY-MM-DD hh24:mi:ss')
This does not seem to work however, and it also looks dodgy, so I didn't really expect it to...
What is the best way to get the entries within my given interval of dates?
You appear to be passing a literal string into the timestamp function - you need to pass in the values as a concatenated string using the concat function. Try the below code snippet
(H.IN_YEAR||H-IN_MONTH||H.ON_DAY||H.ON_HOUR,'YYYMMDDHH24')
I am using MS SQLServer and trying to insert a month/year combination to a table like this:
INSERT INTO MyTable VALUES (1111, 'item_name', '9/1998')
apparently, the above command cannot work since
Conversion failed when converting date and/or time from character string.
Because 9/1998 is a bad format. I want to fix this and this column of the table will show something like:
9/1998
12/1998
(other records with month/year format)
...
Can someone help me with this?
thank you
SQL Server only supports full dates (day, month, and year) or datetimes, as you can see over on the MSDN data type list: http://msdn.microsoft.com/en-us/library/ff848733(v=sql.105).aspx
You can use a date value with a bogus day or store the value as a string, but there's no native type that just stores month/year pairs.
I see this is an old post but my recent tests confirm that storing Date or splitting the year and month to two columns (year smallint, month tinyint) results in the overall same size.
The difference will be visible when you actually need to parse the date to the filter you need (year/month).
Let me know what do you think of this solution! :)
Kind regards
You can just use "01" for the day:
INSERT INTO MyTable VALUES (1111, 'item_name', '19980901')
You can:
1) Change the column type to varchar
2) Take the supplied value and convert it to a proper format that sql server will accept before inserting, and format it back to 'M/YYYY' format when you pull the data: SELECT MONTH([myDate]) + '/' + YEAR([myDate]) ...
You may want to consider what use you will have for your data. At the moment, you're only concerned with capturing and displaying the data. However, going forward, you may need to perform date calculations on it (ie, compare the difference between two records). Because of this and also since you're about two-thirds of the way there, you might as well convert this field to a Date type. Your presentation layer can then be delegated with the task of displaying it appropriately as "MM/yyyy", a function which is available to just about any programming language or reporting platform you may be using.
if you want use date type, you should format value:
declare #a date
SELECT #a='2000-01-01'
select RIGHT( convert (varchar , #a, 103), 7) AS 'mm/yyyy'
if you want make query like SELECT * FROM...
you should use varchar instead date type.
Is there any difference between these two queries?
select * from tbl where ts < '9999-12-31-24.00.00.000000';
and
select * from tbl where ts < timestamp('9999-12-31-24.00.00.000000');
When is the timestamp function required?
Is there a difference in performance?
If ts is a string type:
1st one is comparing like for like as strings
2nd one will cause ts to be converted to date/time
If ts is a date/time type,
1st one will convert the constant to the same date time type as the ts column
2nd one is comparing like for like as date/time
If ts is string type, the 2nd one is worst to use because ts will be converted thus invalidating any indexes.
If ts is date/time, there is no difference
Data type precedence applies to most DB engines
At first glance, the first statement will make a string comparison while the second should make date related comparisons.
It depends on your SQL implementation, although assuming the column "ts" is some date/time type, there's practically no difference.
Most SQL implementations have a cast mapping that instructs the engine how to automatically convert data types that fit some pattern, especially if a literally comparison makes no sense. For example, literally comparing a timestamp to a string doesn't make much sense. However, most engines have a mapping that lets it know a string formatted like a date can be automatically converted to a timestamp, in order to be compared to another timestamps.