I want to get the specific column called date_period but only the date? I want to convert it not to add another column in the result
I tried this:
CONVERT(VARCHAR(10), GETDATE(), 111)
But this will just return another column with the convert of datetime. I have many datetime columns and I just want the date_period to be convert and so I can compare it to another datetime that only has a date and 00:00 time.
Thank you in advance.
EDIT
And by the way the result I get from what I tried is the date today. Like I said I have many datetime columns in one table I just want to convert a single column.
My preferred approach
cast(columnname as date)
Okay it looks like I solved it.
The answer that I found is this
REPLACE(LEFT(CONVERT (varchar, columnname, 101),10)
Thanks for all the effort.
Check this, Use the column that is needed for you either expected_date or expected_datetime like below-
if object_id('tempdb..#test') is not null
drop table #test;
create table #test(ts datetime)
insert into #test values (getdate())
select ts as actual_datetime,
CONVERT(VARCHAR(10),GETDATE(),23) as expected_date,
cast(CONVERT(VARCHAR(10),GETDATE(),23)as datetime) as expected_datetime
from #test
Although you already solved it in your own way but still I would like to add some points for others
You want to extract only date part from the datetime column, there are some formats available for getting only date part from the datetime column.
Please find the list from the given link to get the date formats.LINK
select convert(varchar(20), getdate(), 103) --- DD/MM/YYYY
select convert(varchar(20), getdate(), 102) --- YYYY.MM.DD
select convert(varchar(20), getdate(), 101) --- MM/DD/YYYY
select convert(varchar(20), getdate(), 104) --- DD.MM.YYYY
select convert(varchar(20), getdate(), 106) --- DD MMM YYYY
select convert(varchar(20), getdate(), 107) --- MMM DD, YYYY
select convert(varchar(20), getdate(), 110) --- DD-MM-YYYY
select convert(varchar(20), getdate(), 111) --- YYYY/MM/DD
select convert(varchar(20), getdate(), 1) --- MM/DD/YY
select convert(varchar(20), getdate(), 2) --- YY.MM.DD
select convert(varchar(20), getdate(), 3) --- DD/MM/YY
select convert(varchar(20), getdate(), 4) --- DD.MM.YY
select convert(varchar(20), getdate(), 5) --- DD-MM-YY
select convert(varchar(20), getdate(), 6) --- DD MMM YY
select convert(varchar(20), getdate(), 7) --- MMM DD, YY
select convert(varchar(20), getdate(), 10) --- MM-DD-YY
select convert(varchar(20), getdate(), 11) --- YY/MM/DD
NOTE: This is for older sql version like mentioned sql server 2008
For recent sql version 2012 and above
You can directly use cast with date type data-type.
select cast(getdate() as date)
Related
My date field has value format to be : Feb 15 2019. Is there a way to convert this to MMDDYYYY format?
Desired output: 02152019
Query I tried: SELECT convert(varchar, getdate(), 112) - this query is showing YYYYMMDD format :(
Any help?
select format( convert(date, 'Feb 15 2019'), 'MMddyyyy')
-- results to: 02152019
-- But again, your application is to take care about format!!!
Try this query,
select replace(convert(varchar, getdate(),101),'/','')
You can try this
Select convert(varchar(12), convert(date, 'Feb 15 2019'), 112)
or
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]
This will give output - 20190215.
You can pass different value like 101. For different output see Here.
Is there a function in SQL that formats a date when given the date and format string?
Similar to how .NET's DateTime.ToString(string format) method works?
I'd like to be able to call something like FORMAT(#myDateTime, 'ddMMMyyyy') and have it give me a string formatted as such.
I know the convert function works but it doesn't quite cover all formats. As an example, SQL can do "dd MMM yyyy" but not "ddMMMyyyy"
As far as I know there is no direct function that will convert date in format passed by you if you are on older SQL version. Format function is available starting from SQL 2012.
You can use convert function but it may not always suit your need.
Refer this: https://msdn.microsoft.com/en-IN/library/ms186724.aspx
To answer your question, this will give you date in DD-MMM-YYYY Format:
select REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',','')
For DDMMMYYYY format:
select REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',',''),'-','')
Also, you can check the date formats supported by convert function using this:
select convert(varchar, getdate(), 101)
select convert(varchar, getdate(), 102)
select convert(varchar, getdate(), 103)
select convert(varchar, getdate(), 104)
select convert(varchar, getdate(), 105)
select convert(varchar, getdate(), 106)
select convert(varchar, getdate(), 107)
select convert(varchar, getdate(), 108)
select convert(varchar, getdate(), 109)
select convert(varchar, getdate(), 110)
select convert(varchar, getdate(), 111)
select convert(varchar, getdate(), 112)
select convert(varchar, getdate(), 113)
Studying SQL Server there is something I am not sure of:
A datetime field with the value:
2012-02-26 09:34:00.000
If I select out of the table using:
CAST(dob2 AS VARCHAR(12) ) AS d1
It formats it as:
Feb 26 2012
What I am unsure of his how or why SQL Server formats DateTime like that. If you use datetime2 it does not - anyone know why?
The default date format depends on the language setting for the database server. You can also change it per session, like:
set language french
select cast(getdate() as varchar(50))
-->
févr 8 2013 9:45AM
try this:
select convert(varchar, dob2, 101)
select convert(varchar, dob2, 102)
select convert(varchar, dob2, 103)
select convert(varchar, dob2, 104)
select convert(varchar, dob2, 105)
select convert(varchar, dob2, 106)
select convert(varchar, dob2, 107)
select convert(varchar, dob2, 108)
select convert(varchar, dob2, 109)
select convert(varchar, dob2, 110)
select convert(varchar, dob2, 111)
select convert(varchar, dob2, 112)
select convert(varchar, dob2, 113)
refernces: http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://www.w3schools.com/sql/func_convert.asp
Compatibility Supports Says that
Under compatibility level 110, the default style for CAST and CONVERT operations on time and datetime2 data types is always 121. If your query relies on the old behavior, use a compatibility level less than 110, or explicitly specify the 0 style in the affected query.
That means by default datetime2 is CAST as varchar to 121 format. For ex; col1 and col2 formats (below) are same (other than the 0s at the end)
SELECT CONVERT(varchar, GETDATE(), 121) col1,
CAST(convert(datetime2,GETDATE()) as varchar) col2,
CAST(GETDATE() as varchar) col3
SQL FIDDLE DEMO
--Results
COL1 | COL2 | COL3
2013-02-08 09:53:56.223 | 2013-02-08 09:53:56.2230000 | Feb 8 2013 9:53AM
FYI, if you use CONVERT instead of CAST you can use a third parameter to specify certain formats as listed here on MSDN
In MS SQL Server you can do:
SET DATEFORMAT ymd
case when isdate(inputdate) = 1
then convert(datetime, cast(inputdate,datetime2), 103)
else
case when isdate(inputdate) = 0
then convert(datetime, cast(inputdate,datetime2), 103)
This is my favorite use of 112 and 114
select (convert(varchar, getdate(), 112)+ replace(convert(varchar, getdate(), 114),':','')) as 'Getdate()
112 + 114 or YYYYMMDDHHMMSSMSS'
Result:
Getdate() 112 + 114 or YYYYMMDDHHMMSSMSS
20171016083349100
to change the date format by using sql syntax you should use this query
SELECT DATE_FORMAT(`<columnName>`, '%d/%m/%Y') FROM schemaname.tablename;
ex:-
for suppose i have a schema named as bugloo and the table name is tbl_company
and in this tbl_company i have a column all are in the date format %yy/%mm/%dd and column name is createdDate and the query should like this
SELECT DATE_FORMAT(`createdDate`, '%d/%m/%Y') FROM bugloo.tbl_company;
after running this query my output date would be converted to %dd/%mm/%yyyy
hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)
How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)