How to get DatePart out of datetime result column in SQLite - database

I have a column StartDateTime which has the Value 2/10/2008 6:30:12
I need only the Date part i.e 2/10/2008 to be retrieved.
I tried using strftime() and date() functions in sqlite.

This is not one of the supported date/time formats.
Store your date/times in the format 2008-10-02 06:30:12 instead.

Related

30-oct-2018 00:00:00 change string to iso date in mongo

I have a collection with string filed which has data like '30-oct-2018 00:00:00'
How can i update this filed datatype to Date with out changing its value.
ie; ISODate("2018-10-30T00:00:00.000Z")
You should never store date /time values as string, it's a design flaw. Store always proper Date objects. Natively MongoDB does not support month names, you have to use $switch statement with 12 branches.
Or use moment.js to convert. But then you need to use it in a forEach line by line

TSQL Translation into Snowflake

I have SQL which I run against the on premises database. In the WHERE clause I am narrowing it down to year
WHERE YEAR(SRCSYS_ADD_DATE_TIME)=2020
and I get the results
In Snowflake work sheet I am using
WHERE EXTRACT(YEAR FROM TO_DATE(SRCSYS_ADD_DATE_TIME))=2020
and I am getting this message
Date '2020-05-28-20.42.09.724117' is not recognized'
Please need help.
Snowflake's automatic date-time parsing isn't recognizing your period separated timestamp formatting.
You'll need to explicitly specify a format to read the string during conversion:
EXTRACT(
YEAR FROM
TO_DATE('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF')
) = 2020
bkan.
The problem is the timestamp format. If you don't specify a timestamp format, Snowflake will try to autodetect. In this case it can't, so you'll have to specify the timestamp format:
select year(to_timestamp('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF6'));
If you don't want to specify the timestamp format, your best option is using YYYY-MM-DD HH24:MM:SS.FFFFF (or some other precision of fractional seconds) like this:
select year('2020-05-28 20:42:09.724117'::timestamp);
The second option reformats the timestamp to the standard format, and the ::timestamp syntax casts it to a timestamp.

SQLite: Extract time from string using strftime

I have database column with time (string is used as data type of the column) in this format '30.09.2014 09:03:01'. I'd like to get timestamp from that column using:
SELECT id, strftime('%d.%m.%Y %H:%M:%S', date) AS time FROM table;
But I get empty column with time. I think there is a problem with dots as a separators. Can anyone help me with that?
The strftime format string is used to specify the output format for printing a date that is supplied in one of the standard formats. The input formats accepted are one of the following:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
You may need to do some string manipulation to reformat the date strings into a format that sqlite can interpret. Often it is convenient to reformat the date when inserting the data initially.
Reference sqlite documentation.

date comparison in oracle for the year ends with 00

The date stored in my database is 01-01-1900 for field emp_doj(Data Type DATE).
But while retrieving the data, the value is 01-jan-00, even though formatted with dd-mm-yyyy.
I am comparing retrieved date field with some other date in SQL query.
select *
form persons
where per_join = to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY')
Here the value of to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY') results in 01-01-00, instead of 01-01-1900
I suspect it's your default NLS settings for the IDE you are using that is merely displaying the dates with a two digit year. Oracle does not store dates in any specific format. It stores them in a binary format internally.
Therefore I suggest you check the settings for your IDE and/or database and you will see the NLS date format set to DD-MM-YY, alter this and set it to DD-MM-YYYY and you will see the dates as you wish to see them.
As for your query, why use TO_CHAR and then TO_DATE? If emp_doj is a date field and per_join is also a date then just compare them directly for equality. If you are trying to cut the time portion off your emp_doj values then just use TRUNC().
For examples and reference on NLS: http://www.orafaq.com/wiki/NLS
More here: http://www.oracle.com/technetwork/database/globalization/nls-lang-099431.html
select to_char(emp_doj,'dd-mm-yyyy') from yourtable
I have got some temporary solutions it currently works for me, I simply changed my select query to
select *
form persons
where to_date(per_join,'DD-MM-YYYY')= to_date(i.emp_doj,'DD-MM-YYYY')

regarding ms sql data type

I got a column in which I wanna store date formats that is dd/mm/yy, mm/dd/yy , etc. What data type do I need to use for this column in my table ? DateTime??
oh and also if I want to store Time format like 24.00 hrs..What data type then ?
I need to store the FORMATS like mm/dd/yy or dd/mm/yy not dates like 2010-12-01 or whatever..SO I should use DATeTime only ?
Store the dates as datetime or timestamp or any of the built-in date or date/time datatypes. Don't worry about formatting the value in the db itself. You can format the date using the language of your choice (whatever language you're using to retrieve the information).
If you want to store date and time you need DateTime column type.
To convert it to desired format you can use CAST and CONVERT functions.
To store formats as strings you can use VARCHAR - 'mm/dd/yy'.

Resources