after converting date only getting date not time - sql

I am trying to convert date as mm/dd/yy format in Sql Server 2008 but after conversion i have only got formatted date but in my database there also time . my database value is :
2003-02-28 17:22:43.000
2003-02-28 17:22:43.000
2003-02-28 17:24:26.000
2003-02-28 17:24:26.000
2003-03-24 09:45:25.000
2003-03-24 09:45:25.000
2003-03-24 09:46:36.000
2003-03-24 09:46:36.000
2006-07-20 12:47:31.070
2003-03-27 15:44:53.000
2003-03-27 15:44:53.000
2003-03-27 15:51:56.000
2003-03-27 15:51:56.000
2003-03-27 16:05:03.000
my query for convert date format is
SELECT CONVERT(VARCHAR(20), StatusDate, 101) AS Post_Converted,StatusDesc, RowVersionInitials from PurchaseOrderStatusHistory
and my output is
05/29/2014
05/29/2014
05/29/2014
05/29/2014
date is successfully formatted but the 'time' is missing . please suggest me how would i get the database time also .

For some reason, SQL Server doesn't offer mm/dd/yyyy with the time component. I would advise you to use format 120 -- the ISO standard format -- but you are asking specifically for this.
You can concatenate the values together:
SELECT CONVERT(VARCHAR(20), StatusDate, 101) + ' ' + CONVERT(VARCHAR(20), StatusDate, 108) AS Post_Converted,
StatusDesc, RowVersionInitials
FROM PurchaseOrderStatusHistory;
Instead of 108, you might want 114:
SELECT CONVERT(VARCHAR(20), StatusDate, 101) + ' ' + CONVERT(VARCHAR(8), StatusDate, 114) AS Post_Converted,
StatusDesc, RowVersionInitials
FROM PurchaseOrderStatusHistory;
This gives you the flexibility of adding milliseconds.

CONVERT accepts a third parameter, in your case 101 to specify the format.
Find details here
What you'd need is - assumably - this
SELECT CONVERT(VARCHAR(20), StatusDate, 120)
... or another appropriate output format.
With SQL Server 2012+ there is FORMAT(), but with v2008 you cannot use this...

Related

Change date time format from YYY-MM-DD HH:MM:SS to YYYY.MM.DD HH:MM:SS in SQL Server

I have a table with a column RequestDate with following format 2019-12-01 00:00:00:000. I want to see results like this: 2019.12.01 00:00:00:000.
I used this command
SELECT CONVERT(char(10), RequestDate, 104) AS finaldate
From the above query I am seeing results as 2019.12.01, but I am missing time (00:00:00:000) - how can I keep along with time. I want final results like 2019.12.01 00:00:00:000
I don't think that there is a built-in format specifier for this. But you could do:
replace(convert(varchar, requestDate, 121), '-', '.')
121 gives you format YYYY-MM-DD HH:MM:SS.SSS. You can then replace each occurence of '-' with ':'.
Note that this assumes that you have a datetime datatype to start with. If you have a string, then no need to convert(), you can just replace().

SQL Server 2012 - Convert(varchar,'2018-12-16 17:33:29',105) + ' ' Convert(varchar,'2018-12-16 17:33:29',108) is not showing correct data

I have a datetime datatype column with the data format '16-12-2018 17:33:29' (dd-mm-yyyy hh:mm:ss). So if I get input data as 2018-12-16 17:33:29, the data needs to be converted into '16-12-2018 17:33:29'.
This is the datatype conversion I have been trying,
SELECT
CONVERT(VARCHAR(10), '2018-12-16 17:33:29', 105) + ' ' +
CONVERT(VARCHAR(10), '2018-12-16 17:33:29', 108)
Output is: 2018-12-16 2018-12-16
But if I use Getdate() instead of hardcoded value I'm getting the expected data format:
SELECT
CONVERT(VARCHAR(10), GETDATE(), 105) + ' ' +
CONVERT(VARCHAR(10), GETDATE(), 108)
Output is: 16-12-2018 17:36:18
My question is, why the same datatype conversion query is working if we use getdate() but not in hardcoded values?
Your second query has a date value as second parameter. Your first query has just a string there.
The fact that that string looks like a date is not important as far as SQL is concerned.
Edit.
When your column has a datetime type, then use datetime values to transport data. Try and keep away from strings as often as possible.

T-SQL - Convert datetime to unseparated ISO value

I'm trying to convert the following DATETIME into ISO format:
-- Today's Date
2018-04-16 2:04PM
I plan on parsing this into a char value as NVARCHAR so that I can concatenate it as part of a string.
Desired result should be:
-- Date and time unseparated
20180416140422
After some research I discovered here that
CONVERT(datetime, GETDATE(), 112)
using the 112 format code should get me the format I want but somehow I get the following sample output:
--Formatted using 112 format
Apr 16 2018 2:04PM
Why is this? I simply want to format the DATETIME object unseparated.
Also how would I do this with or without the time tagged onto the end?
Using SQL Server 2008R2
It should be :
select CONVERT(varchar(20), GETDATE(), 112)
Output
20180416
If you want date & time both, then can try using this :
select CONVERT(varchar(20), GETDATE(), 112) + replace(CONVERT(varchar(20), GETDATE(), 108), ':', '')
Output
20180416193327
Something like:
--20180416152520
SELECT REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), GETDATE(), 121), '-', ''), ':', ''), ' ', '') AS DesiredFormat
Output:
20180416152520
Check this out:
SELECT CONVERT(varchar(20), GETDATE(), 112) AS ISODate
, CONVERT(varchar(50), GETDATE(), 126) AS ISOWithTime_WorksWithAnyLanguageSetting;
I believe the names of the columns are self-explanatory.
Output example:
ISODate | ISOWithTime_WorksWithAnyLanguageSetting
------------------------------------------------------
20180416 | 2018-04-16T15:26:50.607
For information on more formats of the CONVERT function with examples check this source.
These all seem a bit overly cumbersome when all you need is format():
select format(getdate(), 'yyyyMMddHHmmss') as ISODateTime;

Issue with Date format in SQL

I am currently using SQL Server 2008. I am extracting a column F254 value from a SQL query where it is returning the date format in MM/DD/YYYY (e.g. 8/17/2017).
I need the output to be in format YYYYMMDD (e.g. 20170817).
Note that the column F254 is of datatype char(10) and I cannot change the datatype.
I have tried below but the getting the needed output
H.F254 AS Original_Date, --> 8/17/2017
CONVERT(VARCHAR(10), H.F254, 111) AS eg1, --> 8/17/2017
REPLACE(CONVERT(VARCHAR(10), H.F254, 103), '/', '') AS eg2 -->8172017
CONVERT(VARCHAR(9), H.F254, 112) AS eg3 --> 8/17/2017
I have also checked the following Date Format but its not working
I think you have to convert it to a date first!
select convert(varchar(10),cast(H.F254 as date),112)

To change date format in sql

I have date in mm/dd/yyyy format in database.I want to display in form as dd/mm/yyyy.
Can anybody help?I want to get time along with date.
CONVERT(VARCHAR(10), YourField, 103)
Per your comment - you want the time as well.
select (CONVERT(VARCHAR(10), YourField, 103) + ' ' + CONVERT(VARCHAR(15), YourField, 108)) as DateTime
http://msdn.microsoft.com/en-us/library/aa226054.aspx
A date value doesn't have a format at all. It gets it's format when you convert it to a string.
You can use the convert function to convert the value in the database, but you should rather leave that to the code in the user interface.
What Guffa said, plus this from books online: http://msdn.microsoft.com/en-us/library/aa226054.aspx
CONVERT(VARCHAR(10), column, 103)
103 is yyyy
3 is yy