DB2 Convert from YYYYMMDD to Date - sql

I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date.
I tried
select cast (DATEFIELD as DATE) as MyDate
But it only returns the old YYYYMMDD format labeled as 'DATE'. What am I doing wrong?

SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"

Use the CONVERT function and the style 112 to get the output in YYYYMMDD
SELECT CONVERT(DATEFIELD, 112) as MyDate
FROM yourtable
Or style 100 for mon dd yyyy hh:mi
SELECT CONVERT(DATEFIELD, 100) as MyDate
FROM yourtable

Simply convert it.
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as MyDate
FROM <your_table>

Related

Convert date (yyyy-mm-dd) to MM-YYYY in SQL

I would like to convert a date in yyyy-mm-dd (Date format, length 10) to mm-yy format.
I have so far tried "right", "Left" and "convert" functions to no avail.
What's the exact syntax I need to use?
SELECT
LEFT (day,7) as 'YYYYMM'
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
SQL Error [3457] [42883]: [Vertica][VJDBC](3457) ERROR: Function LEFT(date, int) does not exist, or permission is denied for LEFT(date, int)
In Vertical, use to_char():
SELECT to_char(day, 'MMYY') as mmyy
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
For SQL database:
SELECT FORMAT(day, 'MM-yyyy') AS 'new_format'
FROM bi_core.fact_campaign_device_stats_daily
WHERE DAY = '2019-07-01'
Or also, to get it numeric:
SELECT YEAR("day") * 100 + MONTH("day") AS yearmonth
FROM bi_core.fact_campaign_device_stats_daily
WHERE "day" = '2019-07-01'

Extract Date in SQL

I have a date format as : 2015-07-11T16:35:02.
I want only the date from the mentioned datetime.ie.(2015-07-11).
How would I convert the date.
Use convert to get result :
SELECT CONVERT(DATE,'2015-07-11T16:35:02',102)
For Comparison use below format :
IF CAST('2015-07-11T16:35:02' AS DATETIME) >= CAST('2015-07-11T16:35:02' AS
DATETIME)
SELECT CAST('2015-07-11T16:35:02' AS DATETIME)
You can use CONVERT :
SELECT CONVERT(DATE, '2015-07-11T16:35:02')
This will result in : 2015-07-11
For the column, you can use :
SELECT CONVERT(DATE, INSERT_DATE)
Hope this helps!!!
Do this. Just done Casting it to date datatype.
SELECT CAST('2015-07-11T16:35:02' AS DATE)
The result will be 2015-07-11
SELECT CONVERT(VARCHAR(10), '2015-07-11T16:35:02')
Checkout this link for more info: MSDN
SELECT CONVERT(varchar(10), INSERT_DATE,102)

convert string to date and add days

I have a string in this format: yyyyMMdd
and i want to add a Year for every date in this format.
How can i do that?
For SQL Server it should be
select
DATEADD(day, your_number_of_day, convert(datetime, your_date_column, 102))
from
your_table

MMYYYY compare to YYYYMMDD

i have dates stored as chars in db (i know, but its not my db nor my idea..).
One of the dates is stored as MMYYYY (012016,022016...) and the second one is stored as YYYYMMDD (20160101,20160202...).
Is there a way to compare those dates? I need to take one date and select all of the second dates which are at least one year older then the first one...
Thank you for any help !
So for example i have Date1 field with values : 012014,012015,012016
and Date2 field with value: 20141005
And i need only Date1 which is at least one year older then Date2 so in this case it will return only 012016
I make two ctes to show how to work it out. I assume MMYYYY are stored as nvarchar or varchar and YYYYMMDD are the same type. If not - you will need one/few conversions on YYYYMMDD field.
;WITH cte AS (
SELECT '012016' as MMYYYY
UNION ALL
SELECT '022016'
), cte2 AS (
SELECT '20160101' AS YYYYMMDD
UNION ALL
SELECT '20160202'
)
SELECT *
FROM cte c
INNER JOIN cte2 c2
ON YYYYMMDD LIKE RIGHT(MMYYYY,4)+LEFT(MMYYYY,2) +'%'
Output:
MMYYYY YYYYMMDD
012016 20160101
022016 20160202
EDIT:
To find out differences in years use this:
DATEDIFF(year,CAST(YYYYMMDD as date), CAST(RIGHT(MMYYYY,4)+LEFT(MMYYYY,2)+'01' as date))
You can convert both fields into type DATE, then compare them.
For MMYYYY, you can use the function DATEFROMPARTS() to build a date. For example, convert 012014 into date:
-- DATEFROMPARTS(year, month, day)
DATEFROMPARTS(RIGHT('012014', 4), LEFT('012014', 2), 1)
For YYYYMMDD, it is easier, because this is the format ISO with Time Style 112. You can use the CONVERT() function to do the conversion :
CONVERT(DATE, '20141005', 112)
And here's how the final query looks like :
WITH casted AS (
SELECT
DATEFROMPARTS(RIGHT(date1, 4), LEFT(date1, 2), 1) AS d1,
CONVERT(DATE, date2, 112) AS d2,
-- ...
FROM yourTable
)
SELECT *
FROM casted
WHERE d1 <= DATEADD(YEAR, -1, d2)
try this
column1 is stored MMYYYY format and column2 is stored YYYYMMDD.
where column1= case when datepart(month,taskDuedate)>9 then convert(varchar,datepart(month,taskDuedate))else '0'+convert(varchar,datepart(month,taskDuedate))end+convert(varchar,datepart(year,taskDueDate))

Convert the Date Format

Using SQL Server 2005
Table1
ID Date
001 01/12/2010
002 15/12/2010
....
DateFormat: dd/mm/yyyy
I want to change the dateformat like mm/dd/yy by using select query
Select id, date from table1
Expected Output
ID Date
001 12/01/2010
002 12/15/2010
....
How to make a query for the above format.
Need Query Help.
You can use convert to specify a mm/dd format, like 101:
select CONVERT(varchar(30), YourColumn, 101)
If your table stores dates as a string, you'd have to convert the string to a date first, like:
select CONVERT(varchar(30), CONVERT(datetime, YourColumn), 101)