Related
I have two problems.
How can I get today's date without time?
How can I convert datetime type into int type?
Just use the following queries:
Q1:How can I get Today's date without time ?
SELECT CONVERT(DATE, GETDATE());
Q2:How can I convert datetime type into int type ?
SELECT YEAR(GETDATE()) * 10000 + MONTH(GETDATE()) * 100 + DAY(GETDATE())
Dates are stored as a decimal value, you did not specify how you want to construct your int.
Do you want the actual decimal value ?
you can see the value like this
select CONVERT(float, getdate())
if you only need the date, then this can help
select convert(int, convert(datetime, convert(date, getdate())))
or
select convert(int, getdate(), 112)
Both methods will return an int value for the given date.
For 22/06/2021 this will return 44367
or if you just want to convert the formatted value, then the easiest way is this
select convert(int, Convert(CHAR(8), getdate(), 112))
For 22/06/2021 this will return 20210622
I am trying to produce a varchar(20) which is 4 character year + 2 character month. I would like to then use this as a parameter in my stored procedure. Im unable to concatenate 2 varchars together to produce this, any ideas?
SELECT
GETDATE() AS CurrentDateTime,
CONVERT(VARCHAR(20), YEAR(GETDATE())) AS CurrentYear,
CONVERT(VARCHAR(20), MONTH(GETDATE())) AS CurrentMonth,
CurrentYear + CurrentMonth AS YearMonth
The simplest approach would be to use this:
SELECT Format(GetDate(), 'yyyyMM') as YearMonth
The function Format() was introduced with Sql Server 2012.
why not simply
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(varchar(6), GETDATE(), 112) AS YearMonth
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) +
Convert(VARCHAR(20),MONTH(GETDATE()))
Column aliases can't be selected from, so the above would do.
Like this one?
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(VARCHAR(20),YEAR(GETDATE())) + Convert(VARCHAR(20),MONTH(GETDATE())) AS YearMonth
Also you can
Year(GetDate()) * 100 + Month(GateDate()) AS YearMonth
or use construction which offered by users below
Convert(varchar(6), GETDATE(), 112) AS YearMonth
I have searched far and wide, but I can't seem find a way to convert julian to yyyy-mm-dd.
Here is the format of my julian:
The Julian format consists of the year, the first two digits, and the day within the year, the last three digits.
For example, 95076 is March 17, 1995. The 95 indicates the year and the
076 indicates it is the 76th day of the year.
15260
I have tried this but it isn't working:
dateadd(d,(convert(int,LAST_CHANGED_DATE) % 1000)-1, convert(date,(convert(varchar,convert(int,LAST_CHANGED_DATE) /1000 + 1900) + '/1/1'))) as GrgDate
You can select each part of the date using datepart()
SELECT DATEPART(yy, 95076), DATEPART(dy, 95076)
+++EDIT: I misunderstood something. Here's my correction: +++++
SELECT DATEADD(day, CAST(RIGHT('95076',3) AS int) – 1, CONVERT(datetime,LEFT('95076',2) + '0101', 112))
Edit: leaving this answer for Oracle and MySQL users
This will not work in T-SQL.
Use this:
MAKEDATE(1900 + d / 1000, d % 1000)
For example:
SELECT MAKEDATE(1900 + 95076 / 1000, 95076 % 1000)
This returns March, 17 1995 00:00:00.
SQLFiddle
I concatenated 20 to my JD and then ran
DATEADD(YEAR, LAST_CHANGE_DATE / 1000 - 1900, LAST_CHANGE_DATE % 1000 - 1)
this got me the result. Thank you!!!
FOR SQL Users
DECLARE #jdate VARCHAR(10)
SET #jdate = 117338
SELECT dateadd(dd, (#jdate - ((#jdate/1000) * 1000)) - 1, dateadd(yy, #jdate/1000, 0))
This will definitely work in all case.
DECLARE #date int
SET #date = 21319
SELECT DATEADD(dd, RIGHT(#date,LEN(#date)-3)-1, DATEADD(yy,LEFT(#date,1)*100 +RIGHT(LEFT(#date,3),2),'1 Jan 1900'))
You need to specify 1 or 0 for century in first character. For example, SET #date = 21319 should be prefixed with 1 or 0. Below is an example that will work with all y2k use cases.
DECLARE #jdate INT
SET #jdate = 119150
SELECT DATEADD(dd, (#jdate - ((#jdate/1000) * 1000)) - 1, DATEADD(yy, #jdate/1000, 0))
Declare #Julian varchar(7)
Declare #date date
Set #Julian = 2020277
Set #Date = Dateadd(day,+ Cast(right(#Julian,3) as int)-1, Cast(left(#Julian,4) + '0101' as Date))
Select #Date
Standard SQL is simple (you can do similar for 2 digit year YYMMDD)
Declare #julDate int = 2020275
Select
DateAdd
(
day
, Right(#julDate,3)-1
, Cast((Left(#julDate,4)+'-01-01') as smalldatetime)
)
Try this out:
DECLARE #jdate int
SET #jdate = 21243
select dateadd(dd, (#jdate - ((#jdate/1000) * 1000)) - 1, dateadd(yy, #jdate/1000+100, 0))
Here is what I have tried thus far:
select CAST(
DATEPART(month,getDate())+'-'+
DATEPART(day,getDate())+'-'+
2012
as datetime)
I end up with the date: 1905-08-02 00:00:00.0. I was expecting to get today's date. I have rearranged the order and it doesn't seem to change. Can anyone offer as to why it gives me this? For the record, I plan to use other values than 2012 for the year.
Thanks in advance.
CAST() each piece as a varchar first:
select
cast(
cast(DATEPART(month,getDate()) as varchar(2))+'-'+
cast(DATEPART(day,getDate()) as varchar(2))+'-'+
'2012' as datetime)
select CAST ('2012'+
CAST(DATEPART(month,getDate()) as char(2))+
CAST(DATEPART(day,getDate()) as char(2))
as datetime)
You have to concatenate strings. Your code is casting the number 2039 to date.
If the goal with this little exercise is to be able to change the year of a given date you can do like this instead.
declare #NewYear int = 2003
-- with time part
select dateadd(year, #NewYear - year(getdate()), getdate())
-- time part removed
select dateadd(year, #NewYear - year(getdate()), dateadd(day, 0, datediff(day, 0, getdate())))
This code will work, you need to make sure that you are concatenating same data types and use convert with specific DateTime Format:
SELECT CONVERT(DATETIME,
CAST(DATEPART(month,getDate()) AS NVARCHAR(50))
+'-'+CAST(DATEPART(day,getDate()) AS NVARCHAR(50))
+'-2012'
,121)
When I select date in SQL it is returned as 2011-02-25 21:17:33.933. But I need only the Date part, that is 2011-02-25. How can I do this?
For SQL Server 2008:
Convert(date, getdate())
Please refer to https://learn.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql
I guess he wants a string.
select convert(varchar(10), '2011-02-25 21:17:33.933', 120)
120 here tells the convert function that we pass the input date in the following format: yyyy-mm-dd hh:mi:ss.
Using CAST(GETDATE() As Date) worked for me
The fastest is datediff, e.g.
select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
from tbl
But if you only need to use the value, then you can skip the dateadd, e.g.
select ...
WHERE somedate <= datediff(d, 0, getdate())
where the expression datediff(d, 0, getdate()) is sufficient to return today's date without time portion.
CAST(
FLOOR(
CAST( GETDATE() AS FLOAT )
)
AS DATETIME
)
http://www.bennadel.com/blog/122-Getting-Only-the-Date-Part-of-a-Date-Time-Stamp-in-SQL-Server.htm
For 2008 older version :
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
you can use like this
SELECT Convert(varchar(10), GETDATE(),120)
In case if you need the time to be zeros like 2018-01-17 00:00:00.000:
SELECT CONVERT(DATETIME, CONVERT(DATE, GETDATE()), 121)
I would use DATEFROMPARTS function. It is quite easy and you don't need casting. As an example this query :
Select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE())) as myNewDate
will return
2021-01-21
The good part you can also create you own date, for example you want first day of a month as a date, than you can just use like below:
Select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1) as myNewDate
The result will be:
2021-01-01
You can try this one too.
SELECT CONVERT(DATE, GETDATE(), 120)
Its too late but following worked for me well
declare #vCurrentDate date=getutcdate()
select #vCurrentDate
When data type is date, hours would be truncated
It's a bit late, but use the ODBC "curdate" function (angle brackes 'fn' is the ODBC function escape sequence).
SELECT {fn curdate()}
Output: 2013-02-01
Convert it back to datetime after converting to date in order to keep same datatime if needed
select Convert(datetime, Convert(date, getdate()) )
If you want to return a date type as just a date use
CONVERT(date, SYSDATETIME())
or
SELECT CONVERT(date,SYSDATETIME())
or
DECLARE #DateOnly Datetime
SET #DateOnly=CONVERT(date,SYSDATETIME())
Use is simple:
convert(date, Btch_Time)
Example below:
Table:
Efft_d Loan_I Loan_Purp_Type_C Orig_LTV Curr_LTV Schd_LTV Un_drwn_Bal_a Btch_Time Strm_I Btch_Ins_I
2014-05-31 200312500 HL03 NULL 1.0000 1.0000 1.0000 2014-06-17 11:10:57.330 1005 24851e0a-53983699-14b4-69109
Select * from helios.dbo.CBA_SRD_Loan where Loan_I in ('200312500') and convert(date, Btch_Time) = '2014-06-17'
select DATE(field) from table;
field value: 2020-12-15 12:19:00
select value: 2020-12-15
In PLSQL you can use
to_char(SYSDATE,'dd/mm/yyyy')
First Convert the date to float (which displays the numeric), then ROUND the numeric to 0 decimal points, then convert that to datetime.
convert(datetime,round(convert(float,orderdate,101),0) ,101)
Try this.
SELECT DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE()))
I would create a scalar function and use format () to set the datatype you want to see. It is must easy on the maintenance later.
Personal favorite:
select convert(datetime, convert(int, getdate()))