Hello guys someone can help me converting this query to work on Oracle ?
SELECT CONVERT(VARCHAR(10),
CAST(DATEADD(DAY,CONVERT(INT,
Convert(nvarchar(50),(ASCII(SUBSTRING(A1_USERLGI,12,1)) - 50))+Convert(nvarchar(50),(ASCII(SUBSTRING(A1_USERLGI,16,1)) - 50))),
'1996-01-01') AS DATETIME),103) FROM SA1010 where A1_USERLGI <> ' ';
This code for decoding a econded field that's save user from system cod and the date of change.
Just with a few changes the Sql can be put in the Oracle flavor.
CONVERT of the date to string becomes TO_CHAR.
SUBSTRING becomes SUBSTR.
+ becomes || or CONCAT.
DATEADD of n days to a date becomes date + n
ASCII remains ASCII.
CAST works for both.
SELECT
TO_CHAR((CAST('01-JAN-1996' AS DATE) +
CAST(CONCAT((ASCII(SUBSTR(A1_USERLGI, 12, 1))-50),
(ASCII(SUBSTR(A1_USERLGI, 16, 1))-50)) AS NUMBER)), 'DD/MM/YYYY') AS dt
FROM SA1010 where A1_USERLGI <> ' ';
Related
I would like to store dates in the format CCYYMMDD in Teradata, but I fail to do so. Find below what I tried so far:
query 1:
SEL CAST(CAST(CURRENT_DATE AS DATE FORMAT 'YYYYMMDD') AS VARCHAR(8))
-- Output: 20191230 ==> this works!
query 2:
SEL CAST(CAST(CURRENT_DATE AS DATE FORMAT 'CCYYMMDD') AS VARCHAR(8))
-- output: SELECT Failed. [3530] Invalid FORMAT string 'CCYYMMDD'.
It seems that the CCYYMMDD is not available in Teradata right away. Is there a workaround?
Tool used: Teradata SQL assistant
Internally, dates are stored as integers in Teradata. So when you say you want to store them in a different format, I don't think you can do that. But you can choose how to display / return the values.
I'm sure there's a cleaner way to get the format you want, but here's one way:
WITH cte (mydate) AS (
SELECT CAST(CAST(CURRENT_DATE AS DATE FORMAT 'YYYYMMDD') AS CHAR(8)) AS mydate
)
SELECT
CAST(
(CAST(SUBSTRING(mydate FROM 1 FOR 2) AS INTEGER) + 1) -- generate "century" value
AS CHAR(2) -- cast value as string
) || SUBSTRING(mydate FROM 3) AS new_date -- add remaining portion of date string
FROM cte
SQL Fiddle - Postgres
You'd have to add some extra logic to handle years before 1000 and after 9999. I don't have a TD system to test, but give it a try and let me know.
Convert DB2 SQL Decimal to time
I need to convert Decimal to time. I have a decimal time field that contains data like this :
123490 --12:34:90
4506 --00:45:06
171205 --17:12:05
etc
I would like to convert into time format then i calculate min between two times columns
Is there anyway to do this with SQL commands or DB2 logic in a select statement?
To convert it to a valid time format you have to do some string manipulations like
cast( substr( right( '00' || '123456', 6) ,1,2) || ':' || substr( right( '00' || '123456', 6) ,3,2) || ':' || substr( right( '00' || '123456', 6) ,5,2) as time)
where 123456 is your decimal. This would work for your 4506 example as well.
You could of cause also use a case statement if you want to avaoid adding the "00" each time.
For calculating the difference in minues there might be other calc options.
You can check out the minutes_between function provided by Db2 11.1
You can use TO_DATE, which is short version for TIMESTAMP_FORMAT, but you first need to convert decimal to string.
select time(to_date(digits(cast(143513 as dec(6,0))),'HH24MISS')) from sysibm.sysdummy1
cast is there just to simulate 6 digit decimal
digits converts decimal to char, padding it with zeros
to_date converts string to timestamp, based on the format string. TIMESTAMP_FORMAT
time returns the time portion of a timestamp
This is based on iSeries version of DB2 but should work for LUW as well.
I am using Sybase IQ and have the following stored as a varchar:
01October 2010
I want to convert this from varchar to date datatype with the following format:
yyyy-mm-dd eg.2010-10-01
How would I write this SQL statement? Thanks in advance.
With difficulty. There's a reason you should never store dates and times as strings.
It's been awhile since I've used Sybase, but what we need to do is get the field into YYYY-MM-DD format, and then pass it to the DATE() or DATETIME() function.
Let's assume the first two characters are always the day of the month, and the last 4 characters are the year. That means everything in between is the month. Let's also assume that there are no leading or trailing spaces. If either of these assumptions fails, then the query will fail.
Then you can do something like this:
SELECT DATE (
RIGHT(UnnamedField,4) + '-' +
CASE LTRIM(RTRIM(SUBSTRING(Unnamed,3,LEN(Unnamed) - 6)))
WHEN 'January' THEN '01'
WHEN 'February' THEN '02'
WHEN 'March' THEN '03'
.
.
.
WHEN 'December' THEN '12'
END + '-' + LEFT(UnnamedField,2)
)
FROM UnnamedTable
Note that, as others have mentioned, the date data type is not a formatted datatype. If possible you should format it in your application. If you must do it in the query, use the CONVERT() function.
Sybase is able to convert a string to a date. So if you use substring to extract the date into a format that IQ can convert, then you can just use the convert() function.
Here's an example of how to do it:
Sample data:
create table #tmp1 (col1 varchar(100))
insert #tmp1 values ('01October 2010')
Query to convert the value to a date:
select
convert
(
date,
(
substring(col1, 3, charindex(' ', col1) - 2) -- Month
+ substring(col1, 1, 2) -- Day
+ substring(col1, charindex(' ', col1), 5) -- Year (include the leading space)
)
)
from #tmp1
Now that the value is in a date format, you can use the convert function to convert the date datatype to string, using your specified format. The default output for a date datatype is yyyy-mm-dd already.
Edit: After taking a look at #BaconBits' answer, I've realized that I could simplify the query a bit by using the substring function wrappers left, right, and the convert wrapper of date. It's the same logic; but using the simplified wrappers might make it easier to understand.
select
date
(
substring(col1, 3, charindex(' ', col1) - 2) -- Month
+ left(col1, 2) -- Day
+ ' ' + right(col1, 4) -- Year (include the leading space)
)
from #tmp1
I am writing a query where I need to calculate the number of days since a date that is stored in the database in the format "YYYYMMDD". Since this is not a Date datatype, I can't use native Date functions. What is the best way (performance-wise, readability-wise, etc.) to perform such a calculation in a SQL query.
Best? Convert that old table to use real date columns.
Next best? Write a database function to convert YYMMDD to a real date. Alan Campin's iDate can help. You'd end up with something akin to select cvty2d(date1)-cvty2d(date2) from ...
Better than nothing? Write ugly SQL to convert the number to character, split the character up, add hyphens and convert THAT to a real date. That beast would look something like
select
date(
substr(char(date1),1,4) concat
'-' concat
substr (char(date1),5,2) concat
'-' concat
substr(char(date1),7,2)
) -
date(
substr(char(date2),1,4) concat
'-' concat
substr (char(date2),5,2) concat
'-' concat
substr(char(date2),7,2)
)
from ...
Edit
The reason these gymnastics are necessary is that the DB2 DATE() function wants to see a string in the form of 'YYYY-MM-DD', with the hyphens being necessary.
Which version of SQL are you running? SQL2008 has no issues with that format as a date datatype
Declare #something nvarchar(100)
set #Something = '20120112'
select dateadd(dd, 1, #Something)
select datediff(dd, #Something, getdate())
2012-01-13 00:00:00.000
118
I need to convert date format in ORACLE SQL Developer
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research.
Here is the command that I consultanted other people on the forum. but it throws me unrecognized command error. table name is B and column name is First
UPDATAE B
set First = concat(to_char(substring(FIRST,1,4) + '-' + substring(FIRST, 6, 2) + '-' + substring(FIRST, 9, 2) + ' ' + substring(FIRST, 12, 8));
Could anyone here help me with it? thanks in advance.
The "unrecognized command" is merely a misspelling of UPDATE:
UPDATAE B
// Should be
UPDATE B
To verify the result is what you expect before executing the UPDATE statement, use a SELECT:
SELECT
to_char(substr(FIRST,1,4) || '-' || substr(FIRST, 6, 2) || '-' || substr(FIRST, 9, 2) || ' ' || substr(FIRST, 12, 8)) AS Test
FROM B
Umm... I'm either missing something extremely obvious or everyone else is.
You want to date operations? Use to_date and to_char. I'm going to assume this ss:sss means, seconds, then fractional seconds. You date appears to be a string so we need to convert it twice:
update b
set first = to_char( to_date( my_date, 'yyyy/mm/dd-hh:mi:ss:ff3')
,'yyyy-mm-dd hh:mi:ss' )
Generally, when using dates it's far, far easier to only use date functions and the provided formats.
As an added point if you have a date, store it as a date. It'll save a world of bother later on.