I have a date field:
Date: YYYY-MM-DD (eg 2002-05-15) that I want to reformat as a date field to read Mon-YY (eg May-02)
when I run the query
select monthname(Date), year(date) from table
I obviously get them as two columns. I've tried
select to_date(monthname(Date) || ' ' || year(Date)) from table
But I get an error. Not sure how to resolve this. I'd like to keep this as a date field if possible.
try this using to_char
select column1, column2, to_char(column2 , 'MMMM DD')
from (values (1, to_date('2002-05-15', 'YYYY-MM-DD')) , (2, to_date('2012-07-15', 'YYYY-MM-DD')) );
Related
I have 1 column that displays year number in the format 1999 and I have another column that displays month number as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
How do I get the single months to display with a 0 in front? I need to combine these 2 columns to display in the form of yyyy/mm so it will be 1999/01 for January 1999.
I tried:
SELECT
YearNumber + '/' + FORMAT(MonthNumber, 'mm') AS PaymentMonth
But I get this error:
Conversion failed when converting the varchar value '/' to data type smallint
Please try:
SELECT
CAST(YearNumber AS varchar(4)) + '/' +
LEFT('0' + CAST(MonthNumber AS varchar(2)), 2) AS PaymentMonth;
Another option, using case when:
The table:
select * from mytable
# YearNumber MonthNumber
# 1999 2
# 2000 11
select YearNumber || '/' ||
(case when MonthNumber < 10 then '0' else '' end) ||
MonthNumber as YearMonth
from mytable
# YearMonth
# 1999/02
# 2000/11
Note: the above works in sqlite, which tends to be more permissive with column types. In SQL Server, if the columns are not strings already then you may need to cast(YearNumber as char(4)) or perhaps use the concat function:
select
concat(YearNumber, '/',
(case when MonthNumber < 10 then '0' else '' end),
MonthNumber) as YearMonth
from mytable
Other DBMSes have different dialects, they may differ slightly.
SQL Date Format with the FORMAT function
Use the FORMAT function to format the date and time data types from a date column (date, datetime, datetime2, smalldatetime, datetimeoffset, etc. data type) in a table or a variable such as GETDATE()
To get DD/MM/YYYY use SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date
To get MM-DD-YY use SELECT FORMAT (getdate(), 'MM-dd-yy') as date.
You can use the concat() function to join them. Depending on the database you can use || instead.
select concat(col1, '/', col2) from tbl;
This is also standard, but not enabled by default on MySQL, and possibly other databases.
select col1 || '/' || col2 from tbl;
Receive date as "19-May" and need to convert it as '05/01/2019', "19-June" to '06/01/2019'.
I have tried various date conversion but it didn't work.
You can try this. Storing date in this format is not a good/suggestion you should use the proper data type which are meant and available for.
You should update the values with proper date time value and then change the data type also. It will save your time & you do not need these conversions every time.
Select Try_Cast('19-May 2019' as Datetime)
OR
Select Try_Cast('19-May' + '2019' as Datetime)
To get the first date of month you can try the below query.
SELECT DATEADD(month, DATEDIFF(month, 0, Try_Cast('19-May 2019' as Datetime)), 0) AS StartOfMonth
Edit
To get the first date of month as per the given data in string, you can use the below query.
declare #dateinStr varchar(20) = '19-May'
Select try_cast('01-' + Replace(#dateinStr, LEFT(#dateinStr, 3), '') + LEFT(#dateinStr, 2) as Datetime) as Date
Here is the demo.
I suppose the months will be 3 chars only, if so then
Select s,
try_Cast(concat(right(s, 3), ' 2019') as Datetime)
from
(
values
('19-May'),
('19-Jun')
) t(s);
If the months is really comes like "June" & "August" then
select s,
try_cast(concat(substring(s, charindex('-',s)+1, 3), ' 2019') as date)
from
(
values
('19-May'),
('19-June'),
('15-August')
) t(s);
If you need to format it as mm/dd/yyyy then use 101 style.
You can do :
SELECT DATEADD(DAY, 1, EOMONTH(CONVERT(DATE, Dates + '-2019'), -1))
FROM ( VALUES ('19-May'), ('19-June')
) t(Dates);
I am struggling to find a solution for the below issue.
date1 = 31-08-2017 12:10:00
I want to cast it as string and need to take date (31-08-2017) alone.
This is my SQL statement:
select *
from table_name
where cast(date1 as varchar) = '2017-08-30'
Here '2017-08-30' is string; when I ran the above select command it's showing o records as date1 is varchar but time also is included.
Can anyone tell me how to split date column alone as a string?
this will help you :-
select * from table_name WHERE CONVERT(varchar(23), [YourDateColumn], 121)= '2017-08-30 00:00:00.000'
above will accept both date and time
if you want to use only date then try this :-
select * from table_name WHERE CONVERT(varchar(10), [YourDateColumn], 20)= '2017-08-30 00:00:00.000'
If your date1's type is DateTime, Try this,
select *
from table_name
where CAST(date1 AS DATE) = CAST('2017-08-30' AS DATE)
This will help you.
select *
from table_name
Where CONVERT(VARCHAR(100),date1,102) = '2017.08.30'
date1 is column name value= '30-08-2017 12:10:00'
Output will be 30-08-2017
I have the data in the following format in varchar form. There are 48 million rows in this format
'2015-09-18 00:00:00.000'
and want to convert it to the following format
'2015-09-18'
Can anyone help me with the code in Oracle
If your column has a timestamp type, you simply need to use to_char to format it properly:
with yourTable(yourDateColumn) as
(
select to_timestamp('2015-09-18 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF') from dual
)
select to_char(yourDateColumn, 'yyyy-mm-dd')
from yourTable
If your column is a string ( and storing dates in string fields is generally a very bad idea) with a fixed format, you simply need a substr:
with yourTable(yourStringColumn) as
(
select '2015-09-18 00:00:00.000' from dual
)
select substr(yourStringColumn, 1, 10)
from yourTable
I have a table where the varchar column CREATED_BY has the data in the format
USER - dd/MM/yyyy hh:mm.
I'm trying to do data migration and need to get records where the created date is greater than a certain date, but the format of the column makes this difficult, so
SELECT * FROM TABLE_NAME WHERE -- last part of CREATED_BY > SOMEDATE
Well, since the dd/MM/yyyy hh:mm format has a fixed length, you can use the RIGHT function to extract the data part from your string:
SELECT *
FROM TABLE_NAME
WHERE CONVERT(datetime, RIGHT(CREATED_BY, 16), 103) > somedate
You need to extract the date from the string:
WHERE cast(reverse ( substring ( reverse ( #string ) , 1 , 16 ) ) as datetime) > somedate