I have an SQLite database containing dates in the following format: 20190621 22:35:18. I'm trying to cast this one into DATETIME to further manipulate it.
Thing is, running the following:
strftime('%Y%m%d %H:%M:%S', my_datetime)
Returns null
I've tried understanding what's going on, by trying to "breaking" the string into smaller parts. So I've run the following:
strftime('%Y', substr(my_datetime, 0, 5))
This time, I've got -470. (Where I would expect to get 2019)
Adding month using the same logic returns: -41609. (Where I would expect to get 2019-06)
Adding any additional information returns null. So I guess that the problem is the way strftime reacts to the year. I have no idea why would SQLite act this way. Any ideas what to do?
Thanks to the comments I understood that SQLite only work with the format: YYYY-MM-DD hh:mm:ss
So I've converted the string to the relevant format using:
UPDATE my_table set my_datetime = substr(my_datetime, 0, 5) || '-' || substr(my_datetime, 5, 2) || '-' || SUBSTR(my_datetime, 7, 2) || SUBSTR(my_datetime, 9);
And now everything works.
Related
My DB contains a period(month) and a year - I am trying to convert it to a date. I don't care for the actual day of the month so I have just made it "1" (the 1st of the month).
In my code I had to convert the "13th" period to the 12th because of the 12 months of the year, so my decode did that part... Ultimately, I want it to return as a date. I have a concatenation to make it 'look' like a date, but not actually a date.
What i do with the data is query it and return it in Excel for further manipulation. When imported to Excel, it does not import as a date nor does it let me convert to a date format.
SELECT DIA_PROJECT_DETAIL.FY_DC || '/' ||
decode(DIA_PROJECT_DETAIL.PER_DC,1,1,2, 2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,12)||
'/01' as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL
There has to be an easier or more effective way to do this!
There is no much simpler way. DECODE is fine for converting month 13 to month 12, but you use it a bit too complicated. Then you shouldn't rely on session settings, but explicitly tell the DBMS the date format your assembled string represents. Use TO_DATE with the appropriate format.
select
to_date(fy_dc || to_char(decode(per_dc, 13, 12, per_dc), '00') || '01', 'yyyymmdd')
as "Date"
from ams.dia_project_detail dia_project_detail;
Just use least():
SELECT (DIA_PROJECT_DETAIL.FY_DC || '/' ||
LEAST(DIA_PROJECT_DETAIL.PER_DC, 12) ||
'/01'
) as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL;
I have a set of data that I am importing into SqlLite, but don't really have the opportunity to manipulate it before insertion. I am trying to calculate "age" of the date, but this is proving very difficult in its current format.
I am looking for a select that I can use to update the data and then begin writing queries the way I want.
Data Samples
9/20/1983
2/18/1986
8/1/1994
5/29/1999
Desired
1983-09-20
1986-02-18
1994-08-01
1999-05-29
Once I have data in that format, I will calculate the date using the following
(strftime('%Y', 'now') - strftime('%Y', Born)) - (strftime('%m-%d', 'now') < strftime('%m-%d', BOrn))
I guess if there's a way to cast the date into the right format and calculate the age in one query, that would save a step, but I haven't been able to find a way so far.
You could use the following update statement to change the date format to YYYY-MM-DD:
update t
set Born =
substr(Born, -4) || '-' ||
substr('0' || substr(Born, 1, instr(Born, '/')-1), -2) || '-' ||
substr('0' || substr(Born, instr(Born, '/')+1,
length(Born)-5-instr(Born, '/')), -2)
where substr(Born, -5, 1) = '/'
and Born LIKE '%/%/%'
The where clause is there to only update dates that have a d/m/yyyy format, where d and m could be two digits as well.
I'm pretty new to Sybase and am writing a query to return results after a specified date, and also before a specified date. MM/DD/YYYY format
At the moment im doing..
SELECT *
From aTable
WHERE afterDate >= 08/07/2013
AND beforeDate <= 08/08/2013
I'm getting records back, but as I'm a Sybase newbie, I want to be sure Sybase is interpreting these dates correctly..
Their online doc is pretty bad for basic explanations on things like this!
Anyone able to confirm if what I have works, or does it need some formatting round the dates?
You'll need to convert the dates into DATETIME and tell sybase what the format is to be sure.
According to this documentation the code for MM/DD/YYYY is 101, so something like this:
SELECT *
FROM aTable
WHERE afterDate >= CONVERT(DATETIME,'08/07/2013',101)
AND beforeDate <= CONVERT(DATETIME,'08/08/2013',101)
You can see the difference by running the following select statements:
SELECT CONVERT(DATETIME,'08/07/2013',101) --MM/DD/YYYY (2013-08-07 00:00:00.000)
SELECT CONVERT(DATETIME,'08/07/2013',103) --DD/MM/YYYY (2013-07-08 00:00:00.000)
For any date-time field in sybase, instead of going through the convert function, there is a more direct approach.
SELECT *
From aTable
WHERE afterDate >= '2013-08-07'
AND beforeDate <= '2013-08-08'
The date has to be in the form 'YYYY-MM-DD'
If you want to add a time, it can be included along with the date. The date and the time have to be separated by a T.
Any date time field can be directly used using the format 'YYYY-MM-DDTHH:MM:SS'
Using the functions is too lengthy. Noone needs a bazooka to shoot a squirrel! :)
CAST( '2000-10-31' AS DATE )
will convert from text to date format....
I am assuming that your two fields (afterDate and beforeDate) are in Date format.
Your example would be:
SELECT *
From aTable
WHERE afterDate >= CAST( '08/07/2013' AS DATE )
AND beforeDate <= CAST( '08/08/2013' AS DATE )
Also, usually (but not always) a date range is on the SAME field. As I said, that is not true all the time and you may have a good reason for that.
The best approach is to use the ANSI standard which does not require any conversion: yyyymmdd (you can also include hh:mm:ss) for instance:
DateField1 >= "20150101" and DateFile1 <= "20150102"
You should decide which Input-Strings the user is going to use as parameter and then convert them and concatenate them like you want, unless it is Datetime it is not important which initial format it had, you can use it in a between-condition.
E. g. the user is from Europe and uses "DD.MM.YY" and "hh:mm" as an input parameter, I would convert and concatenate like this:
WHERE dateCol between convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.06.14', 4), 16) || ' ' || '00:00', 8)
AND convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.07.14', 4), 16) || ' ' || '16:00', 8)
I need to convert date format in ORACLE SQL Developer
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research.
Here is the command that I consultanted other people on the forum. but it throws me unrecognized command error. table name is B and column name is First
UPDATAE B
set First = concat(to_char(substring(FIRST,1,4) + '-' + substring(FIRST, 6, 2) + '-' + substring(FIRST, 9, 2) + ' ' + substring(FIRST, 12, 8));
Could anyone here help me with it? thanks in advance.
The "unrecognized command" is merely a misspelling of UPDATE:
UPDATAE B
// Should be
UPDATE B
To verify the result is what you expect before executing the UPDATE statement, use a SELECT:
SELECT
to_char(substr(FIRST,1,4) || '-' || substr(FIRST, 6, 2) || '-' || substr(FIRST, 9, 2) || ' ' || substr(FIRST, 12, 8)) AS Test
FROM B
Umm... I'm either missing something extremely obvious or everyone else is.
You want to date operations? Use to_date and to_char. I'm going to assume this ss:sss means, seconds, then fractional seconds. You date appears to be a string so we need to convert it twice:
update b
set first = to_char( to_date( my_date, 'yyyy/mm/dd-hh:mi:ss:ff3')
,'yyyy-mm-dd hh:mi:ss' )
Generally, when using dates it's far, far easier to only use date functions and the provided formats.
As an added point if you have a date, store it as a date. It'll save a world of bother later on.
I need to get various parts of a TIMESTAMP field - specifically year, localized month name (in Russian), day of month and hours interval (like '11 - 12').
Currently i came up with this:
select
extract (year from prt.dtbegin) as f_year,
(
case extract (month from prt.dtbegin)
when 1 then 'Январь'
when 2 then 'Февраль'
/* ... */
when 12 then 'Декабрь'
end
) as f_month,
cast (lpad (extract (day from prt.dtbegin), 2, 0) as char(2)) as f_day,
(
cast (lpad (extract (hour from prt.dtbegin), 2, 0) as char(2))
|| ' - '
|| cast (lpad (extract (hour from prt.dtbegin) + 1, 2, 0) as char(2))
) as f_hour
from prt
It works fine (interval '23 - 24' is OK at the moment), but I don't like it, especially CASE sentence with each and every month.
So, i'd like to know, is there any common way of getting localized month names in Firebird? Also, can i format extracted parts of timestamp, instead of current cast-lpad-extract construct?
Thanks in advance.
What about a reference table with the localised strings in them with a key of the month number. I thought this was done in the Firebird system tables, but have not been able to find documentation or the location in the system tables, if indeed it is there.
EDIT:
I have checked the system tables, and the documentation, and the literals are not available within Firebird itself. Sorry :-)
Instead of CASE you can use DECODE builtin function: http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-decode.html
I don't think there's a built-in feature for that. You should consider writing your own UDF for that. Once done, it's very simple to use. The following resources should point you in the right direction:
http://www.firebirdsql.org/index.php?op=useful&id=deatz_udf
http://www.firebirdfaq.org/faq83/
http://www.ibphoenix.com/downloads/writing_internal_udfs.pdf
I sure hope next major release (3.0) will support writing internal functions.
Update
Firebird 3.0 will support internal SQL Functions: http://tracker.firebirdsql.org/browse/CORE-2047