I want to change date format to date time using substring - sql-server-2008

I have date format like 20151030
I want to change it 30/10/2015
12:23:43 PM . Can anyone please advise on this.

You can use the following query
select CONVERT(varchar(25), CONVERT(datetime, CAST(20151030 AS CHAR(10)), 113), 103)
The output would be 30/10/2015
You can also visit This URL For a list of date formatting specifications.
This SO Post discusses on the similar lines about date formatting.

Related

Converting Date to String and then to Date again in mysql .Combining 2 functions in mysql

have column which is of type date having default format as YYYY-MM-DD. I want to get date in format DD-MM-YYYY but as date type only. I would like to know how is it possible? I am combining 2 functions together as shown below but i am not getting the desired result
STR_TO_DATE(DATE_FORMAT(emp.doj, '%d-%m-%y'),'%d-%m-%y') as doj
Please dont close this questions as its a different one not asked before thanks!!!
DATE_FORMAT(emp.doj,'%d-%m-%Y') as doj
You have not need to use STR_TO_DATE. use only DATE_FORMAT. It should be work.

How can we change date format in MySQL

I want to change date format in mysql.I tried this code
DATE_FORMAT(STR_TO_DATE(mydate, '%d.%m.%y'), '%Y-%m-%d')
But its not hep me.
My date comes from as string like this:
DD-MM-YYYY
I want to change it to this:
YYYY-MM-DD
How can I achive this with mysql?
Thanks
Would:
STR_TO_DATE(mydate, '%y.%m.%d')
work then because when your are creating the date from the string you are implicitly giving it a format. Then just call it in a different format.
SELECT str_to_date(table_column_name,'%d-%m-%Y') FROM `table_name`

Setting the Mysql DateFormat

I want to change the data format in the mysql database like in this format 'April 11, 1979' this is what i got from the facebook API while retrieving from the facebook
So how to set the date format (April 11, 1979) while creating the mysql table.
You can't change the way that mysql store dates, instead you can change the date format returned from facebook and then store it in MYSQL, you can do something like this:
$fb_date = strtotime($date_returned_from_facebook);
$insert_in_my_sql = date('Y-m-d',$fb_date);
read more about PHP date: http://php.net/manual/en/function.date.php
If you just want to accept something like the specified string as a date imput format then this might help (see here: Parse date in MySQL):
STR_TO_DATE('April 11, 1979', '%M %e, %Y') AS date
With date formats explained here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format .
You can use STR_TO_DATE function for this.
INSERT INTO test
VALUES (1, STR_TO_DATE('April 11, 1979', '%M %d,%Y') ) ;
Fiddle - http://www.sqlfiddle.com/#!2/db30d/1

Converting SQL system datetime to non-standard format

All,
I've been wracking my brains trying to figure this one out. I need to convert SQL timestamp to a non-standard format that I haven't been able to find an easy way to convert to.
I need to convert:
1900-01-01 00:00:00.000
to
1/1/1900 12:00 AM
I've tried several combos of datepart with casting, but I haven't had much luck. I suppose I'll need to convert a couple of times to get what I want, but I figured I would ask the experts first.
Thanks!
Are you just trying to format a date into a string? If so take a look at this page:
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
Towards the bottom of the page is something that looks just like what you are looking for:
SELECT CONVERT(VARCHAR(20), SYSDATETIME(), 22) --> 06/08/11 1:30:45 PM
Well I ended up finding the desired result with a colleague.
Here is what it ended up being, just in case anyone else needs it.
SELECT CONVERT(VARCHAR(10), EDITDATE, 101) + ' ' + RIGHT(CONVERT(VARCHAR, EDITDATE,100),7) AS ConvertedDate

How to get time (hh:mm:ss) from sql query?

I have Database with date field.
I see the time like: 1900-01-01 13:38:00.000
How i can format it like: 13:38 ?
SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss.
If you are using SQL server 2008 then,
Suppose your column name is XTime which is holding time in HH-MM-SS.ms formate. If we want to retrive time in HH-MM-SS format then write following query,
Select ID, XDate, Convert(Time(0),XTime,0) -- For HH-MM-SS format
Try this:-
RIGHT(CONVERT(VARCHAR,'DATE',100),7)
For non-military time.
I know I am late. This has already been answered by #Yes - That's Jake. It works perfect. But to help you more, I have a cheat sheet link so that you can write it in any format.
You can visit: Date and Time conversions using SQL cheat sheet
You will find a list of codes and the format that #Yes - That's Jake mentioned in his answer.