How to convert decimal to date? - sql

I have a problem. I have a field START_VALUE where the data type is decimal. I want to convert Decimal to Date.
The data type START_VALUE is Decimal.The data of START_VALUE is :
START_VALUE
|=========|
| 240368 |
| 198448 |
| 197396 |
| 126743 |
I want to convert to Datetime and what i want to expect is like 'yyyy-mm-dd 00:00:00.000'
I don't know if the value is Julian date or else. Please help me to find the right select query.
Thanks.

Are you looking for this
CREATE FUNCTION Number_To_Date(No_Of_Days NUMBER)
RETURN DATE IS
BEGIN
RETURN to_date('1899-12-30', 'YYYY-MM-DD') + No_Of_Days;
END;

You can use the following query in SQL Server to parse it to datetime
SELECT CONVERT(DATETIME, SUBSTRING(CAST(START_VALUE
as varchar(8)),1,2) + '-' + SUBSTRING(CAST(START_VALUE
as varchar(8)),3,2) + '-' + SUBSTRING(CAST(START_VALUE
as varchar(8)),5,2),5) AS DateTimeValue
FROM Table
If you are using Oracle then you can use the following query
SELECT TO_DATE(CAST(START_VALUE AS VARCHAR(8), 'DDMMYY') FROM Table

Related

replace string with date value

Currently, I have a Derived Column transformation within my package that will look for a NULL value and if it is NULL it will give it a default date with forwarding slashes as seen below
REPLACENULL([date],"2/2/1999")
However, if that field is Not NULL it will have a string date which will look like 20200202. I am wanting to add on to the current expression to where if the field is not null that it replaces 20200202 with 2020-02-02. I would appreciate the help and will rate it highly!
Here's how I would do that in SQL Server.
Note: FORMAT requires SQL Server 2012+
DECLARE #StringDate varchar(20);
-- NON-NULL date.
SET #StringDate = '20200202'
SELECT
ISNULL ( FORMAT( CAST ( #StringDate AS date ), 'yyyy-MM-dd' ), '2/2/1999' ) AS NonNullStringDate;
-- NULL date.
SET #StringDate = NULL;
SELECT
ISNULL ( FORMAT( CAST ( #StringDate AS date ), 'yyyy-MM-dd' ), '2/2/1999' ) AS NullStringDate;
Returns (respectively)
+-------------------+
| NonNullStringDate |
+-------------------+
| 2020-02-02 |
+-------------------+
+----------------+
| NullStringDate |
+----------------+
| 2/2/1999 |
+----------------+
For date formatting consistency, you may want to consider changing 2/2/1999 to 1999-2-2.
First of all, you should use 1999-02-02 as a default value to make sure that all values are stored in the same format. The following expression will convert the date format from yyyyMMdd to yyyy-MM-dd:
REPLACENULL([date],"") == "" ? "1999-02-02" : LEFT([date],4) + "-" + SUBSTRING([date],5,2) + "-" + RIGHT([date],2)

Converting Date Format in SQL Server - Getting Data Type Error

Trying to convert my date to a different format but running into issues.
Currently, my date column looks like:
YearBuilt
1934-01-01 00:00:00:0000
1981-01-01 00:00:00:0000
I'd like to have it be:
YearBuilt
01/01/1934 00:00:00:0000
01/01/1981 00:00:00:0000
I tried
update table set YearBuilt = '01/01/' + YearBuilt
But get the error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
But my YearBuilt column is already a datetime data type, so I'm not sure which string it's trying to convert.
Any input would be sincerely appreciated!
Date/times are stored in SQL in an internal format. That is a good thing! You should specify the format on output. That is, you can do:
select '01/01/' + datename(year, YearBuilt)
You can build this into the table:
alter table t add YearBuilt_str as ('01/01/' + datename(year, YearBuilt))
Then just use the string version to get the alternative format.
Try this:
DECLARE #D DateTime = '2017-11-01 00:00:00:000';
SELECT #D AS Input,
CONVERT(VARCHAR(50) , #D, 101) +
SUBSTRING(CONVERT(VARCHAR(50), #D, 113), 12, LEN(CONVERT(VARCHAR(30), #D, 113))) AS OutPut;
Result:
+---------------------+------------------------+
| Input | OutPut |
+---------------------+------------------------+
| 01.11.2017 00:00:00 | 1/11/2017 00:00:00:000 |
+---------------------+------------------------+
When your column has Date Time datatype, then you can't update the date with your custom date format.
But, you can select your date with your Custom format by SQL Query:
SELECT '01/01/'+CONVERT(VARCHAR(4), YEAR(YearBuilt))+' '+CONVERT(VARCHAR(MAX), CAST(YearBuilt AS TIME)) [YearBuilt]
FROM <table_name>;
Result :
YearBuilt
01/01/1934 00:00:00.0000000
01/01/1981 00:00:00.0000000

Date subtraction error

I have a SQL Server table with a few columns.
One of those columns is a date and another is No of Nights.
Number of nights is always a two character varchar column with values like 1N, 2N, 3N etc depending on the number of nights up to 7N.
I want to subtract the 1 part of the 1N column from the date.
For ex: 25Oct15 - 1N = 24Oct15
Obviously I will be replacing the '1N' with the actual column name. I tried doing a trim as:
date - left(no of nights, 1)
But I get an error
Conversion failed when converting the varchar value '25Oct16' to data type int.
Sample date below
Date | NoofNIghts | Result
2016-04-26 00:00:00.000 | 1N |
2016-04-28 00:00:00.000 | 3N |
Where the result column would be the subtracted value. Any help would be great. Thanks.
SELECT DATEADD ( DAY, - CONVERT(INT, REPLACE(NoofNights, 'N', '')), getdate() ) as Result
Try this
DECLARE #V_Date DATETIME = '2016-04-26 00:00:00.000'
,#V_NoofNIghts VARCHAR(2) = '1N'
SELECT DATEADD(DAY, CAST(LEFT(#V_NoofNIghts,1) AS INT) *-1 ,#V_Date)
Well basic query should be like
Update tablename
set result= DATEADD(d, -CAST(LEFT(NoofNIghts, LEN(NoofNIghts)-1) AS INT),Date)

Two questions for formatting timestamp and number using postgresql

I am selecting a date column which is in the format "YYYY-MM-DD".
I want to cast it to a timestamp such that it will be "YYYY-MM-DD HH:MM:SS:MS"
I attempted:
select CAST(mycolumn as timestamp) from mytable;
but this resulted in the format YYYY-MM-DD HH:MM:SS
I also tried
select TO_TIMESTAMP(mycolumn,YYYY-MM-DD HH:MM:SS:MS) from mytable;
but this did not work either. I cannot seem to figure out the correct way to format this. Note that I only want the first digit of the milliseconds.
//////////////second question
I am also trying to select numeric data such that there will not be any trailing zeros.
For example, if I have values in a table such as 1, 2.00, 3.34, 4.50.
I want to be able to select those values as 1, 2, 3.34, 4.5.
I tried using ::float, but I occasionally strange output. I also tried the rounding function, but how could I use it properly without knowing how many decimal points I need before hand?
thanks for your help!
It seems that the functions to_timestamp() and to_char() are unfortunately not perfect.
If you cannot find anything better, use these workarounds:
with example_data(d) as (
values ('2016-02-02')
)
select d, d::timestamp || '.0' tstamp
from example_data;
d | tstamp
------------+-----------------------
2016-02-02 | 2016-02-02 00:00:00.0
(1 row)
create function my_to_char(numeric)
returns text language sql as $$
select case
when strpos($1::text, '.') = 0 then $1::text
else rtrim($1::text, '.0')
end
$$;
with example_data(n) as (
values (100), (2.00), (3.34), (4.50))
select n::text, my_to_char(n)
from example_data;
n | my_to_char
------+------------
100 | 100
2.00 | 2
3.34 | 3.34
4.50 | 4.5
(4 rows)
See also: How to remove the dot in to_char if the number is an integer
SELECT to_char(current_timestamp, 'YYYY-MM-DD HH:MI:SS:MS');
prints
2016-02-05 03:21:18:346
just add ::timestamp without time zone
select mycolumn::timestamp without time zone from mytable;

How to convert string ddmmyyyy to datetime fomat (DD/MM/YYYY) from provider IBMDA400

I have a problem when I try converting data from column name:'date'(string: ddmmyyyy ex:09032015) to fomat datetime (DD/MM/YYYY) and I'm using provider IBMDA400.
I use some commands to try converting but have not been successful.
My purpose is only select data from database but Visual Studio always displays error.
ex:
Cmd1: select date(to_date(my string,'dd/mm/yyyy') from table
-> In here appear error: SQ20448: Expression not valid using format string specified for TIMESTAMP_FORMAT.
And I changed: 'TIMESTAMP_FORMAT' instead of 'TO_DATE'
cmd2: select TIMESTAMP_FORMAT(mystring, 'DD/MM/RRRR HH24:MI')
-> Error SQ20448 continue to occur.
you have indicated through comments below that there are 3 string fields (for day, month and year) and that you concatenate these so they look like 09032015
Instead concatenate in an ISO standard fashion and then convert to date.
For DB2 I would try this:
to_date( ( CONCAT z CONCAT '-' CONCAT Y CONCAT '-' CONCAT X) , 'YYYY-MM-DD' )
where
Z = year string
Y = month string
x = day string
Once you have a date you can reformat it to any desired style you like.
Please note that date data types are NOT STORED in any particular format.
This following was prepared on the (wrong) assumption of the dbms being Oracle.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE TABLE1
(X date, Y timestamp, Z varchar2(60))
;
INSERT ALL
INTO TABLE1 (X,Y,Z)
VALUES (to_date('09032015','ddmmyyyy'), to_timestamp('09032015','ddmmyyyy'), '09032015')
SELECT * FROM dual
;
Query 1:
SELECT
TO_CHAR(X, 'DD/MM/YYYY')
, TO_CHAR(Y, 'DD/MM/YYYY')
, substr(Z,1,2) || '/' || substr(Z,3,2) || '/' || substr(Z,5,4)
FROM TABLE1
Results:
| TO_CHAR(X,'DD/MM/YYYY') | TO_CHAR(Y,'DD/MM/YYYY') | SUBSTR(Z,1,2)||'/'||SUBSTR(Z,3,2)||'/'||SUBSTR(Z,5,4) |
|-------------------------|-------------------------|-------------------------------------------------------|
| 09/03/2015 | 09/03/2015 | 09/03/2015 |