SQL Column Numeric to DATE - sql

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.

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')

HIVE select where date between, but its a string YYYYMMDD, not numeric date format?

so I'm trying to learn how to deal with this.
the key partition column is "date" as a string (YYYYMMDD).
so its a character string, not a numeric date format.
I'm having trouble understanding how I can WHERE filter off this by normal date ranges like
where date > CURRENT_DATE -30
or
WHERE date BETWEEN '20210512' AND '20210517'
How does this work? Should the date column be converted first?
This is SQL in a HIVE database.
The best way is to convert the comparison date to a string. Many databases support to_char():
where date > to_char(current_date - 30, 'YYYYMMDD')
Those that do not would have some other function to convert the date to a string.
Note that this formulation allows the use of indexes and table partitions to optimize the query.

Converting Abbreviated Date to Date

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

How to convert the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

How to convert date from Germany to local date in sql?

I have a column in a table with time stamps from Germany (Utc + 6) in their local time (Utc -1). How do I convert all those datetimes in my local time?
I think the correct way of converting UTC datetime into local datetime is using CLR function. You can find an example below.
https://www.mssqltips.com/sqlservertip/2339/converting-utc-to-local-time-with-sql-server-clr/
There are -7 hour between germany and central time zone in us
CT (-6)
Berlin (+1)
If your column is datetime USE THE FOLLOWING
Select DATEADD(HOUR,-7,[DATECOLUMN])
Update 1
Consider that #bdate and #edate are the begin and end date of the daylight savings so you can use this query
Select case when DATEADD(HOUR,-7,[DATECOLUMN]) between #bdate and #edate Then
DATEADD(HOUR,-8,[DATECOLUMN]) else
DATEADD(HOUR,-7,[DATECOLUMN]) end
If your data is in a string format like "dd.mm.yy," then you would use the code page 104 to tell SQL how to parse your date from string to a DateTime format.
CONVERT(DateTime, DateField, 104)
If your string is in some other format, then look up the correct codepage in the table here (CAST and CONVERT in T-SQL):