need to get date from a name in sql server - sql

im trying to get a date from a name i have got the date but i need it to be in yyyy-mm-dd format.
what i get:
20221201
what i need:
2022-12-01
please help me on this

Could be as simple as this ... assuming the date string is the last 8 characters
Example
Select try_convert(date,right('typographical_dismal_subjoinByapostrophize1_12345_20221201',8))
Resutls
2022-12-01

This should work ,
SELECT CONVERT(DATE,RIGHT('ABCDEF20221201',8),103)

Related

Is there a snowflake function that gives the timestamp format as 'YYYYMMDDHH24MISS'?

We have a timestamp column that as values as below, But user expects it to be in the format of 'YYYYMMDDHH24MISS'. Is there any way we can get it.
Date
2022-03-01 16:34:02
2022-06-14 22:14:01
2022-01-06 23:57:05
Tried below query but didnt work.
select to_timestamp(cast(date as string),'YYYYMMDDHH24MISS') from table
Error - Can't parse '2022-03-01 16:34:02' as timestamp with format 'YYYYMMDDHH24MISS'.
Try to_varchar(timestamp_col, 'YYYYMMDDHH24MISS').
Sample code -
select to_varchar('2013-04-05 01:02:03'::timestamp, 'YYYYMMDDHH24MISS')

How to get previous month from 'yyyy-MM' date format in Hive

I want to get previous month from 'yyyy-MM' format value in Hive. I tried with 'add_months' but couldn't able to get.
select add_months('2021-06', -1)
Eg: I have one string column having value '2021-06', now I want to display output as '2021-05'.
Please help on this.
Convert your string to date by concatenating with '-01', add_months, use date_format to get yyyy-MM:
select date_format(add_months(concat('2021-06','-01'), -1),'yyyy-MM')
Result:
2021-05
Another method (using substr instead of date_format):
select substr(add_months(concat('2021-06','-01'), -1),1,7) --returns 2021-05

Convert date from string to date type in Hive

I want to change string which is in format '29-MAR-17' to date type in Hive. The column in question is named "open_time".
I have tried using:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP('open_time', 'dd/MM/yyyy')));
But it returns NULL. Subsequently, my objectif is to do something like this :
SELECT * FROM table_hive WHERE open_time BETWEEN '29-MAR-17' AND '28-MAR-17';
With strings, it will definitely not work.
Any help please ?
This should work
select to_date(from_unixtime(unix_timestamp('29-MAR-17','dd-MMM-yy')))
Returns 2017-03-29

Get Formatted Date in SQL Server CE

How to get the formatted date in SQL Server CE?
I have a column on a table that contains Date, but the column type is nvarchar
ID Date
----------------------
1 05/08/2012
2 10/08/2012
3 05/10/2012
The date format is MM/dd/yyyy, but it is in nvarchar.
Also, I want to have a WHERE clause to select the ID on a specific date. How can I do that? This is for SQL Server CE. Thank you very much.
you need to cover it use convert or cast function ...
cast(datecolumn as DateTime)
or
CONVERT(datetime,datecolumn,101)
For current date, you can use:
SELECT CONVERT(nvarchar,GETDATE(),101)
Output: (As of answered date)
05/31/2016

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...