teradata SQL convert real to timestamp - sql

I can't understand how to convert this type of real - 42389.520752314813 to timestamp.
I got this data from one source, but I need to convert it to normal timestamp format.

I think you have received wrong input data.
this type of timestamp is only occurring when Destination tool is excel and which has CELL as "Number" type, and during Copy-Paste of timestamp, destination field has Calculated it as Mathematical function. Please re-verify your source. I am sure about this mathematical calculation. please check the below sample of such data. So practically you cannot perform its reverse operation.

Hard to do without knowing what timestamp should be returned.
If the date is sourced from a SQL Server it might be '2016-01-22 12:29:53':
cast( date '1900-01-01' + myCol as timestamp(3))
+ (cast(86400 * (myCol mod 1) as dec(12,6)) * interval '00:00:01.000000' hour to second)
If it's from Excel it's two days earlier and you must start at '1899-12-30'

Related

Issues while converting timestamp to specific timezone and then converting it to date in bigquery

I am doing just a simple conversion of timestamp column value to specific timezone and then getting the date out of it to create analytical charts based on the output of the query.
I am having the column of type timestamp in the bigquery and value for that column is in UTC. Now I need to convert that to PST (which is -8:00 GMT) and was looking straight forward to convert but I am seeing some dates up and down based on the output I get.
From the output that I was getting I took one abnormal output and wrote a query out of it as below:
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10" ,"-8:00")) as completed_date1,
Date(Timestamp("2021-05-27 18:10:10","America/Los_Angeles")) as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10","America/Tijuana")) as completed_date4
The output that I get is as below:
Based on my understanding I need to subtract 8 hours from the time in order to get the timestamp value for the timezone that I wanted and according to that completed_date3 column seems to show the correct value that should be there but if I use other timezone conversions as suggested in google documentation, the output gets changed to 2021-05-28 and I am not able to understand how that can happen.
Can anyone let me know what is the thing that I am doing wrong?
I was actually using it in a wrong way. I need to use it as below :
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10") ,"-8:00") as completed_date1,
Date(Timestamp("2021-05-27 18:10:10"),"America/Los_Angeles") as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10"),"America/Tijuana") as completed_date4
Initially I was converting that string timestamp to a specific timestamp based on the timezone and that is what I did not want.
Now if a convert a string to timestamp first without using time zone parameter and then apply timezone parameter when getting the date value out of it then it would return me correct date.
Please see the snapshot below :

How to add a certain number of days based on field value

I'm working on creating a view to convert some values that we get coming in for dates/times. (I've figured out the times already.) In this table, dates come in the format of "Days after 12-31-1840." I'm trying to create a view that shows the actual dates/times rather than in this format. This is what I have so far:
CASE WHEN UPPER(FLWSHT_MEAS_NM) LIKE '%DATE' THEN TO_CHAR(DATE '1840-12-31' + INTERVAL ACUTE_MEASURE_VALUE DAY)
I know that this is the correct syntax for adding dates, because I'm able to get the view working with this instead:
CASE WHEN UPPER(FLWSHT_MEAS_NM) LIKE '%DATE' THEN TO_CHAR(DATE '1840-12-31' + INTERVAL '30' DAY)
My question is, how do I add a specific number of days based on the ACUTE_MEASURE_VALUE field? I'm not able to run the code to get a runtime error as it's coming up as a syntax error.
From the Teradata documentation:
Teradata SQL extends the ANSI SQL:2011 standard to allow the operations of adding or subtracting a number of days from an ANSI DATE value. Teradata SQL treats the number as an INTERVAL DAY value.
I'm assuming your field ACUTE_MEASURE_VALUE is already in your table and is an integer. The words INTERVAL and DAY are part of the specification of interval constants - this is a variable and syntactically you don't use those keywords.
...TO_CHAR(DATE '1840-12-31' + ACUTE_MEASURE_VALUE)...
Just drop the INTERVAL keyword and the DAY keyword and it should work.
By the way, why are you using To_Char() in this? By transforming it into a character string it preempts anyone using this view from performing calculations on this date. If you leave the view in DATE format then any subsequent Select from this view has a lot more flexibility in manipulating this data field.

SQL date as bigint

I have a DB with date looks like BIGINT "15321600".
It's represent 17/2/2017
How can I convert it?
edit:
I understood that the number represent the minute passed from 1/1/1988...
You really need logic to specify what the conversion is. My trial-and-error, it looks like minutes since 1988-01-01 produces your value.
That means that you can use logic like this
select date '1988-01-01' + col * interval '1' minute
Of course, date/time functions vary by database, so the exact syntax might be different in your database.

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.

Insert only Month and Year date to SQL table

I am using MS SQLServer and trying to insert a month/year combination to a table like this:
INSERT INTO MyTable VALUES (1111, 'item_name', '9/1998')
apparently, the above command cannot work since
Conversion failed when converting date and/or time from character string.
Because 9/1998 is a bad format. I want to fix this and this column of the table will show something like:
9/1998
12/1998
(other records with month/year format)
...
Can someone help me with this?
thank you
SQL Server only supports full dates (day, month, and year) or datetimes, as you can see over on the MSDN data type list: http://msdn.microsoft.com/en-us/library/ff848733(v=sql.105).aspx
You can use a date value with a bogus day or store the value as a string, but there's no native type that just stores month/year pairs.
I see this is an old post but my recent tests confirm that storing Date or splitting the year and month to two columns (year smallint, month tinyint) results in the overall same size.
The difference will be visible when you actually need to parse the date to the filter you need (year/month).
Let me know what do you think of this solution! :)
Kind regards
You can just use "01" for the day:
INSERT INTO MyTable VALUES (1111, 'item_name', '19980901')
You can:
1) Change the column type to varchar
2) Take the supplied value and convert it to a proper format that sql server will accept before inserting, and format it back to 'M/YYYY' format when you pull the data: SELECT MONTH([myDate]) + '/' + YEAR([myDate]) ...
You may want to consider what use you will have for your data. At the moment, you're only concerned with capturing and displaying the data. However, going forward, you may need to perform date calculations on it (ie, compare the difference between two records). Because of this and also since you're about two-thirds of the way there, you might as well convert this field to a Date type. Your presentation layer can then be delegated with the task of displaying it appropriately as "MM/yyyy", a function which is available to just about any programming language or reporting platform you may be using.
if you want use date type, you should format value:
declare #a date
SELECT #a='2000-01-01'
select RIGHT( convert (varchar , #a, 103), 7) AS 'mm/yyyy'
if you want make query like SELECT * FROM...
you should use varchar instead date type.