I have a query that results in a timestamp value along with certain other calculations.
The result looks something like below -
City DateTime Value
London 2009-01-01 00:00:00.000000 22
New York 2010-01-01 00:00:00.000000 33
... ... ...
Is there any way to obtain the dateTime column with month and year - something like Jan-2009 and Jan-2010 instead of entire timestamp. I don't want to use the case statement.
t=# select now(),to_char(now(),'Mon-YYYY');
now | to_char
-------------------------------+----------
2017-09-20 07:49:34.360483+00 | Sep-2017
(1 row)
https://www.postgresql.org/docs/current/static/functions-formatting.html
to_char(timestamp, text)
and
https://www.postgresql.org/docs/current/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE
for formats
the postgresql data type formating function to_char can solve this problem. It takes 2 arguments, a timestamp and a template pattern string, and return a date string according to the provided pattern. see Postgresql documentation for the complete pattern list.
You can try something like the following:
select city, to_char(your_date_field, 'Mon-YYYY'), value from your_table
Related
I have this code in Teradata that reads "x_date/100+190000". So from my understanding it removes the 'day' portion from DATE and then adds an INT number of days. Now I have to translate the same into BigQuery but can't see how.
edit: so what I have is a SELECT statement that includes the "x_date" field, which has a DATE format. It contains a list of dates in the form of 'yyyy-mm-dd'. The query reads something like:
SELECT x_date/100+190000
FROM x_table
and the field has this sort of rows:
| '2022-06-06' |
| '2020-03-06' |
| '2019-09-01' |
| '2028-05-06' |
What I don't understand exactly is what this functions are doing in Teradata.
My expected output should be in DATE format and should be copying (in BigQuery), whatever the Teradata function is doing to the field.
Use below
SELECT FORMAT_DATE('%Y%m', x_date)
FROM x_table
In my Oracle DB, I have a date field called HIGH_DATE. The format for some entries is "27-SEP-12" (DD-MON-YY) and for some entries it is "27-09-12" (DD-MM-YY).
Can someone help me in framing a select query through which I can get dates in either formats??
If you have a DATE column then it does not have any format; it is stored internally as 7-bytes (century, year-of-century, month, day, hour, minute, second) and it is only when the user interface being used to access the database returns data to the user that it then gets formatted (and all the dates will be implicitly converted to strings with a consistent format).
I'm going to assume that when you say:
I have a date field called "HIGH_DATE"
What you actually mean is: "I have a column with a VARCHAR2 data-type where I store date values".
If that is the case then all you need to do is:
SELECT TO_DATE( high_date, 'DD-MM-RR' ) AS high_date
FROM table_name;
Oracle's string-to-date conversion rules will match additionally the MON format model if you use the MM format model and don't specify an exact match using the FX format model.
If you have the test data:
CREATE TABLE table_name ( high_date ) AS
SELECT '23-09-20' FROM DUAL UNION ALL
SELECT '15-AUG-99' FROM DUAL;
Then the above query will output (depending on your NLS_DATE_FORMAT):
| HIGH_DATE |
| :------------------ |
| 2020-09-23T00:00:00 |
| 1999-08-15T00:00:00 |
db<>fiddle here
However, the best solution is going to be to stop storing the values as strings and to store them (without a format) as a date.
I have the below Table. Is it possible to turn the Month column into a date?
ID Month
1 201805
Expected Results:
Id Month Date
1 201805 5/1/2018
You can just use to_date() with a suitable format mask, e.g. with a fixed value as a demo:
select to_date(to_char(201805), 'YYYYMM') from dual;
TO_DATE(TO_CHAR(201
-------------------
2018-05-01 00:00:00
You don't need to explicitly add a day number to the value (as #HoneyBadger showed, or by concatenating after converting to a string), because it will default to the first of the month anyway.
So you would need to do something like
to_date(to_char(month), 'YYYYMM')
The to_char() part could be skipped as the number would be implicitly converted to a string anyway, but it's more complete to include it.
It isn't clear if you intend to do this as part of a query (maybe in a view) or want to modify the table; if the latter then you could use a virtual column to avoid duplicating data and having to maintain the converted value if the month changes.
Trying the change the date column from YYYYMMDD to MMDDYYYY while maintaining varchar value. Currently my column is set as varchar(10). Is there a way to change the strings in mass numbers because I have thousands of rows that need the format converted.
For example:
| ID | Date |
------------------------
| 1 | 20140911 |
| 2 | 20140101 |
| 3 | 20140829 |
What I want my table to look like:
| ID | Date |
------------------------
| 1 | 09112014 |
| 2 | 01012014 |
| 3 | 08292014 |
Bonus question: Would it cause an issue while trying to convert this column if there is data such as 91212 for 09/12/2012 or something like 1381 which is supposed to be 08/01/2013?
Instead of storing the formatted date in separate column; just correct the format while fetching using STR_TO_DATE function (as you said your dates are stored as string/varchar) like below. Again, as other have suggested don't store date data as string rather use the datetime data type instead
SELECT STR_TO_DATE(`Date`, '%m/%d/%Y')
FROM yourtable
EDIT:
In that case, I would suggest don't update your original table. Rather store this formatted data in a view or in a separate table all together like below
create view formatted_date_view
as
SELECT ID,STR_TO_DATE(`Date`, '%m/%d/%Y') as 'Formatted_Date'
FROM yourtable
(OR)
create table formatted_date_table
as
SELECT ID,STR_TO_DATE(`Date`, '%m/%d/%Y') as 'Formatted_Date'
FROM yourtable
EDIT1:
In case of SQL Server use CONVERT function like CONVERT(datetime, Date,110). so, it would be (Here 110 is the style for mm-dd-yyyy format)
SELECT ID,convert(datetime,[Date],110) as 'Formatted_Date'
FROM yourtable
(OR)
CAST function like below (only drawback, you can't use any specific style to format the date)
SELECT ID, cast([Date] as datetime) as 'Formatted_Date'
FROM yourtable
MS SQL Server Solution:
Which SQL are you trying with?
MSSQL Server 2008 R2
You can use Convert function on your date field. You have to specify the date's format Style.
For mm/dd/yyyy format Style value is 101.
Using with style value, your update statement can be:
UPDATE table_name
SET date = CONVERT( VARCHAR, date, 101 )
Refer To:
How to format datetime & date in Sql Server
SQL Server 2008 Date Format
Demo # MS SQL Server 2008 Fiddle
MySQL Solution:
it needs to stay in varchar or int and the dates are yyyymmdd and I need to change thousands of rows of data to be in mmddyyyy format.
Change to date type using str_to_date and then change again to string using date_format.
UPDATE table_name
SET date = DATE_FORMAT( STR_TO_DATE( date, '%Y%m%d' ), '%m%d%Y' )
The value 20140911 when converted from yyyymmdd to mmddyyyy format, will retain the leading 0 as 09112014.
Bonus question: Would it cause an issue while trying to convert this column if there is data such as 91212 for 09/12/2012 or something like 1381 which is supposed to be 08/01/2013
You can use str_to_date( '91212', '%c%e%y' ) to convert the same to valid date object. But MySQL, though defines to support single digit month and date numbers, it won't parse such date correctly and returns a NULL on such formats.
mysql> select str_to_date( '91212', '%c%e%y' ) s1, str_to_date( '091212', '%c%e%y' ) s2;
+------+------------+
| s1 | s2 |
+------+------------+
| NULL | 2012-09-12 |
+------+------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------+
| Warning | 1411 | Incorrect datetime value: '91212' for function str_to_date |
+---------+------+------------------------------------------------------------+
1 row in set (0.00 sec)
I have a table with 2 columns, EffectiveBeginDate and EffectiveEndDate. However, they're in a number YYYYMMDD format so for example:
Agent | EffectiveBeginDate | EffectiveEndDate
John Doe | 20080922 | 20080924
Mark Smith| 20100922 | 20110226
etc. etc.
I need to do 2 things. First, I need to have a formula to change the EffectiveBegin/End dates to an actual date format in my Select statement.
More importantly, I need to be able to query this column in my where statement as if they were acutal dates b/c the where clause is going to be something like:
Where EffectiveStartDate between '01/01/2010' and '01/31/2010'
Thoughts?
In SQL SERVER it is sufficient to perform CONVERT(DATETIME, dateTimeField) when dateTimeField is in the format "yyyymmdd".
Update:
For numeric dates, use this:
CONVERT(DATETIME, CONVERT(char(8),dateTimeField) )