Converting Abbreviated Date to Date - sql

I have a truncated "date" attribute in the dataset, and need to convert the value below to a full DATE format (assuming it's the current year).
SELECT '6-May'
would output:
2020-05-06

SQL Server is very flexible about recognizing date formats. If you want to produce a date datatype, you can cast as follows:
select cast(mycol + '-2020' as date) mydate from mytable

Related

how to do a SELECT in SQLite with a range of dates from datetime string to a UNIX timestamp?

Hi I have a database SQLite with a table "data" with the column "time#timestamp" that is a REAL for example 1669729394.792
So I have to select a range of data using 2 dates (start date and end date) written by the operator in human datetime format (ex. 2022-11-29) and extract all my data
somehow I should convert my date from standard format to UNIX timestamp
I tried like this but it doesn't work for me:
SELECT * FROM data WHERE date([time#timestamp]) BETWEEN CAST(strftime('%s', '2022-11-29') AS REAL) AND CAST(strftime('%s', '2022-11-30') AS REAL)
The function date() with a numeric parameter and no modifiers considers the numeric value as a Julian day and returns that date in the text format YYYY-MM-DD.
But your datetime values are not Julian days, they are unix timestamps and you can transform them to a readable date format YYYY-MM-DD with the modifier 'unixepoch':
date([time#timestamp], 'unixepoch')
After that you can directly compare the result to any date in the format YYYY-MM-DD and no casting is needed:
SELECT *
FROM data
WHERE date([time#timestamp], 'unixepoch') BETWEEN '2022-11-29' AND '2022-11-30'
Or, keep the value of [time#timestamp] as it is and transform the 2 date boundaries to unix timestamps:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN strftime('%s', '2022-11-29') AND strftime('%s', '2022-11-30')
Or, if your version of SQLite is 3.38.0+:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN unixepoch('2022-11-29') AND unixepoch('2022-11-30')

How do I convert a date timestamp in oracle to a date?

I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool.
For example if i want data of june month ? how can i fetch ?
You can use the TO_DATE(…) function to treat the VARCHAR column as a formatted date type, and the EXTRACT(…) function to retrieve just the month out of the date.
If your date string is formatted in a well-known manner, TO_DATE's automatic parsing (or a direct cast using the :: operator) will suffice, and you can write your query this way:
SELECT * FROM table
WHERE
EXTRACT(month, TO_DATE(varcharCol)) = 6 -- June of every year
AND EXTRACT(year, varcharCol::DATE) = 2020; -- June of 2020 alone
Alternatively, if the date is in a non-standard format, use available formatting options to make TO_DATE(…) parse it properly:
-- Dates of custom format, such as: 'June # 02 # 2020'
SELECT
EXTRACT(month, TO_DATE(varcharCol, 'MMMM # DD # YYYY')) AS month
FROM table
WHERE
month = 6;
Note: You can also swap all DATE and TO_DATE above with TIMESTAMP and TO_TIMESTAMP if the data carries a whole timestamp value within it instead of only a date.
First of all, you shouldn't store dates as strings. But you probably know that.
If you do store dates as strings, you store them all in one particular format, say, 'mm/dd/yyyy'. So, use a substring function to get the month digits.
For 'mm/dd/yyyy':
where substring(date_string, 1, 2) = '06'
For 'yyyy-mm-dd':
where substring(date_string, 9, 2) = '06'
In many situations you can also use LIKE:
For 'mm/dd/yyyy':
where date_string like '06%'
For 'yyyy-mm-dd':
where date_string like '%-06-%'
You have to use to_date in snowflake to convert varchar datatype to date as following
select *
from yourTable
where to_date(yourDateColumn, 'YYYY-MM-DD') >= '2020-06-01'
and to_date(yourDateColumn, 'YYYY-MM-DD') <= '2020-06-30'

convert int YYYYMMDD to date AS400

I need to convert an integer to a date format in AS400.
I have the field called ivdat8, which is integer in the formatted YYYYMMDD.
I need to use a WHERE clause to select data between two dates, today and 3 days ago.
I am using the below line to do this:
Where
ivdat8 Between (Select current date - 3 days from sysibm.sysdummy1) And (Select current date from sysibm.sysdummy1)
The current date from sysibm is a true date format, but ivdat is integer.
How can i cast ivdat8 to be a date format i can use within the WHERE clause ?
I have tried the below to convert the int to date:
cast(cast(A.ivdat8 as varchar(50)) as date)
&
TIMESTAMP_FORMAT(ivdat8,'YYYYMMDD')
Actually it's better for performance not to convert the ivdat8 column data, but do this with parameters like below.
select ivdat8
from table (values
int(to_char(current date - 2 days, 'YYYYMMDD'))
, int(to_char(current date - 5 days, 'YYYYMMDD'))
) t (ivdat8)
where ivdat8 between
int(to_char(current date - 3 days, 'YYYYMMDD'))
and int(to_char(current date, 'YYYYMMDD'));
Easiest way to do it without causing a complicated conversion in the query is to use this:
cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date)
The full where clause doesn't need to select from sysibm.dummy1 either.
where cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date) between current date - 3 days and current date
If you have indexes built on ivdat8 though, the fastest selection will be:
where A.ivdat8 between cast(Current date - 3 days as dec(8)) and cast(Current Date as dec(8))
Managed to convert the int to a date, first i had to cast it as a string, then use TO_DATE
To_Date(cast(A.ivdat8 as varchar(50)),"YYYYMMDD")
try this
Where cast(TIMESTAMP_FORMAT(cast(ivdat8 as varchar(8)), 'YYYYMMDD') as date)
Between (current date - 3 days) and current date

SQL Column Numeric to DATE

I am trying to convert numeric column (with 13 digits) to DATE and all i can do is this so i can see the date as a string.
CONVERT(VARCHAR(10),(DATEADD(SECOND, Start_Date/1000 ,'1/1/1970')),104)
What can i do so this will end up like DATE so i can filter it later?
The database is MS SQL.
Thanks in advance!
This expression should convert it to a datetime:
DATEADD(SECOND, Start_Date/1000 , '1970-01-01)
If you want a date, just convert this to a date:
CAST(DATEADD(SECOND, Start_Date/1000 , '1970-01-01') as DATE)
Note: I changed the date format to the ISO standard YYYY-MM-DD format.