SQL SERVER DATETIME FORMAT - sql

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

Related

How can I convert a specific column of datetime into date only?

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)

Varchar date conversion in SQL

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.

Converting GETDATE() to Hijri date to yyyymmdd

I am trying to get the Hijri GETDATE() and convert it into this format yyyymmdd
I have already tried this script
SELECT CONVERT(VARCHAR(10), GETDATE(), 131)
but it gives me this format ( 16/06/1438 ) and what I actually need is (1438/06/16)
SQL Server does not offer a wealth of formatting options for such dates, so just construct it yourself:
SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) + '/' +
CONVERT(VARCHAR(5), GETDATE(), 131)
) as hj_yyyymmdd
Oops. Right idea, wrong implementation:
SELECT (RIGHT(CONVERT(VARCHAR(10), GETDATE(), 131), 4) +
SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 131), 3, 4) +
LEFT(CONVERT(VARCHAR(10), GETDATE(), 131), 2)
) AS hj_yyyymmdd
Use below query :
SELECT FORMAT ( GETDATE(), 'yyyy/MM/dd', 'ar-SA' )
Another option is to convert to varchar then date then varchar again.
Using format() with 'ar-SA' seems to return 1 day earlier than using convert() with style 131.
select Method='multiconvert'
,conversion = convert(varchar(10)
,convert(date,convert(varchar(12),getdate(),131),103),112)
union all
select 'format'
, format ( getdate(), 'yyyyMMdd', 'ar-SA' )
union all
select 'style131'
,convert(varchar(12),getdate(),131)
rextester demo: http://rextester.com/LIX82417
returns
+--------------+--------------+
| Method | conversion |
+--------------+--------------+
| multiconvert | 14380616 |
| format | 14380615 |
| style131 | 16/06/1438 |
+--------------+--------------+
If you are having SQL Server 2012 and above,
SELECT FORMAT(GETDATE()+1,'yyyy/MM/dd','ar')
It will give you the below result for the date 2017/03/14
1438/06/16
Try with 120:
SELECT CONVERT(VARCHAR(10), GETDATE(), 120)
You can do that using the following query:
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),'-','/')

Extract date from datetime column - SQL Server Compact

I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.
My column signup_date is a DateTime with a value 04-09-2016 09:05:00.
What I've tried so far without success:
SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date
I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Y-m-d?
SELECT DATEPART(month, signup_date)
SQL Server Compact has no date type.
If you don't want to see the time, convert the datetime value to a string:
SELECT CONVERT(nvarchar(10), GETDATE(), 120)
(This has been tested and actually works against SQL Server Compact)
you were actually on track with the CAST function just a slight error in the syntax. In the CAST function, there needs to be 'as' i.e CAST(data_registo as date)
SELECT CAST(data_registo as date) AS signup_date;
Most of the answers seek to achieve same thing but the explanation to the codes is not enough
CONVERT(date, Date_Updated, 120)
this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc.
The second item 'Date_Updated' is the name of the column to be converted.
the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD.
Hope this helps
The old fashioned way of doing this in SQL Server might work for your purposes:
select dateadd(day, datediff(day, 0, signup_date), 0)
The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.
If you don't like 0 as a date, you can put any valid date in its place:
select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')
EDIT:
If you simply don't want to see the time, convert the date to a string:
select convert(nvarchar(10), signup_date, 120)
(I recommend the YYYY-MM-DD format, but others are available.)
I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.
SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0,
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "
This will return only date value in original datetime type. So you can do any comparison using the output
SELECT convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120))
Just saw the Question today, a bit late I know :) but maybe this will help..,
select convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))
select convert(nvarchar, getdate(), 1) = 09/25/19
select convert(nvarchar, getdate(), 2) = 19.09.25
select convert(nvarchar, getdate(), 3) = 25/09/19
select convert(nvarchar, getdate(), 4) = 25.09.19
select convert(nvarchar, getdate(), 5) = 25-09-19
select convert(nvarchar, getdate(), 6) = 25 Sep 19
select convert(nvarchar, getdate(), 7) = Sep 25, 19
select convert(nvarchar, getdate(), 10) = 09-25-19
select convert(nvarchar, getdate(), 11) = 19/09/25
select convert(nvarchar, getdate(), 12) = 190925
select convert(nvarchar, getdate(), 23) = 2019-09-25
select convert(nvarchar, getdate(), 101) = 09/25/2019
select convert(nvarchar, getdate(), 102) = 2019.09.25
select convert(nvarchar, getdate(), 103) = 25/09/2019
select convert(nvarchar, getdate(), 104) = 25.09.2019
select convert(nvarchar, getdate(), 105) = 25-09-2019
select convert(nvarchar, getdate(), 106) = 25 Sep 2019
select convert(nvarchar, getdate(), 107) = Sep 25, 2019
select convert(nvarchar, getdate(), 110) = 09-25-2019
select convert(nvarchar, getdate(), 111) = 2019/09/25
select convert(nvarchar, getdate(), 112) = 20190925
select convert(nvarchar, getdate(), 8) = 13:48:36
select convert(nvarchar, getdate(), 14) = 13:49:48:713
select convert(nvarchar, getdate(), 24) = 13:49:57
select convert(nvarchar, getdate(), 108) = 13:50:07
select convert(nvarchar, getdate(), 114) = 13:50:14:490
select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM
select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM
select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307
select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49
select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923
select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM
select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473
select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM
select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM
select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740
select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50
select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153
select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627
Use this, i had the same problem
SELECT CAST(data_registo as date) AS "signup_date"
To get a string value, use CONVERT
select convert(varchar(10), signup_date, 11)
Check here for various formats:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
To get a DATE, and just strip out the time, do this
Select Cast (signup_date as DATE)

SQL VarChar to Date

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)