T-SQL convert BigInt into Date - tsql

I used to work this way :
select cast(cast(20171002 as varchar) as date) AS BigInt_into_Date,
but is it possible, to achive the same with single use of convert() function ?
Conversion between bigint and datetime is explicite, so expression argument in convert is needed, so mayby we also could convert into date

It is possible to convert from BigInt to DateTime.
However, the numeric value for October 2nd 2017 is 43008 (days since Jan 1, 1900).
20171002 is really a string representation of a date stored as a number - hence your need for the double cast.
And no - I don't think it's possible to perform any simple arithmetic to get from 20171002 to 43008 :-)

Related

How to truncate date in postgres?

I am using below condition to truncate date in postgres
to_date(to_char(trunc(appointment_date),'YYYYMMDD')||appointment_end_time,''YYYYMMDDHH24:MI:SS')AS tq
How I can use this in postgres ?
Strange data typing, sometimes requires strange, looking at least, queries. Try (see fiddle)
date_trunc('day',appointment_date)
+ substr(appoinment_end,12)::interval
As your to_char() call uses the format 'HH24:MI:SS' for the "time" column, you can cast that column directly to a time value, e.g. using the :: operator: appointment_end_time::time.
To build a new timestamp from the date part of the appointment_date and the time value, just add them:
appointment_date::date + appointment_end_time::time
So first the timestamp is converted to a date (that does not have a time), and then the time value is added to that, which yields a timestamp.
Note that to_date() returns a date so your code would remove the just added time part again. You would need to use to_timestamp() if you really want a timestamp as the result.
To answer the question's title "how to truncate date in Postgres?" (which in reality refers to a timestamp not a date): you can either cast it to a date (see above) or you can use date_trunc() (not trunc()) with a unit to which it should be truncated. However, date_trunc returns a timestamp not a date value, so you couldn't add a time to the result.

Converting long (numbers) text datatype (with blank values) to date

I have troubles converting HubSpot UNIX timestamp to date. Timestamp is stored as text value.
Value looks like this:
1549324800000
My logic was first to convert the number to bigint and later converted it to date using:
TO_CHAR(TO_TIMESTAMP(properties__vape_station__value)), 'DD/MM/YYYY')
What would be the best way to achieve converting UNIX Timestamp in text type to date in PostgreSQL 11.
You can cast the value in that column to a bigint and then use to_timestamp.
But apparently that column also stores empty strings rather than NULL values if no value is present, so you need to take that into account:
to_timestamp(nullif(trim(properties__vape_station__value),'')::bigint/1000)
This would still fail however if anything else than a number is stored in that column.

Converting Netezza timestamp to Julian Day Number

I have been looking for it during days but could not find how to do..
It is like:
select to_number(to_char('2015-06-24 00:00:00','J')) on Oracle.
I need to find the Julian Numeric Day value, not to be confused with the ordinal date of the year..
Conversion templates indicate that 'J' is exactly what you want.
I think the issue you have is the to_number() function, not the to_char() function. Use casts instead.
SYSTEM(ADMIN)=> select to_char('2015-06-24 00:00:00'::timestamp,'J')::int;
?COLUMN?
----------
2457198
(1 row)
You need to use the DDD (day of year) date identifier in TO_CHAR.
Reference: date and time constants.

TSQL: Convert varchar 'dd/mm/yy' to DATETIME

A stupid third-party software that we use stores datetime as varchar for some good old reason and I need to parse it as sql datetime. Problem is, when the string is in mm/dd/yy format plain CAST() as datetiem works fine but my data is formatted as dd/mm/yy and CAST throws a
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
exception. Tips on doing it with CONVERT or CAST without using RIGHT()/LEFT() etc?
Thanks
One option would be to use SQL Server's SET DATEFORMAT setting before performing the conversions. e.g.
-- Set date format to day/month/year.
SET DATEFORMAT dmy;
GO
DECLARE #datevar datetime2 = '31/12/2008 09:01:01.1234567';
SELECT #datevar;
GO

How to convert d/MM/yyyy data to dd/MM/yyyy in sql server table?

I have create one field in sql server database as nvarchar datatype and store some date like 'd/MM/yyyy' and 'dd/MM/yyyy' format previously. Now i want to get all data in 'dd/MM/yyyy' format using query it is possible?
You can cast the field to datetime in the query:
select cast(YourField as datetime)
from YourTable
where isdate(YourField) = 1
The where isdate(YourField) = 1 part is necessary to filter out rows where the value is no valid date (it's a nvarchar field, so there could be things like abc in some rows!)
But you should really change the field to datetime in the long term, as already suggested by Christopher in his comment.
Casting like described above is always error-prone because of the many different data formats in different countries.
For example, I live in Germany where the official date format is dd.mm.yyyy.
So today (December 9th) is 9.12.2011, and running select cast('9.12.2011' as datetime) on my machine returns the correct datetime value.
Another common format is mm/dd/yyyy, so December 9th would be 12/9/2011.
Now imagine I have a nvarchar field with a date in this format on my German machine:
select cast('12/9/2011' as datetime) will return September 12th (instead of December 9th)!
Issues like this can easily be avoided by using the proper type for the column, in this case datetime.