getdate() function [duplicate] - sql

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
How can I display only date while using getdate() function?
When I am using
select getdate() as Today's Date
It is giving me:
2016-05-18 08:32:07.100
But I only need:
2016-05-18
How to get this?

Use convert. The first argument truncates the length, and the last specifies the format you want the date in:
SELECT Convert(varchar(10), getdate(), 103) as [Today's Date]

If you are using SQL Server 2012+, you may also want to look at FORMAT - which in my opinion is a bit nicer to read/use.
Example: SELECT FORMAT(GETDATE(), 'dd/MM/yyyy')
See this msdn link for more info

Related

Int to date in SQL [duplicate]

This question already has answers here:
Convert an Int to a date field
(2 answers)
Closed 3 years ago.
I try to convert the column [policyeffective_date] which looks like YYYYMMDD (for example 20190430) into a date in SQL Server.
I tried a few solutions find on the forum, but none worked. Currently I have the following code and errors:
SELECT
CONVERT(DATETIME, policyeffective_date, 104) AS test
FROM
[DATAWAREHOUSE].[dbo].[blabla]
Error:
Arithmetic overflow error converting expression to data type datetime.
I also tried:
SELECT
CONVERT(DATE, CONVERT(NVARCHAR(8), policyeffective_date), 104)
FROM
[DATAWAREHOUSE].[dbo].[blabla]
Error:
Operand type clash: int is incompatible with date
Thank you for your help
You would seem to have bad data in the column. Given that you are getting a run-time error and not a compile-time error, the types seem correct.
The simplest solution is try_convert():
SELECT try_convert(datetime, policyeffective_date, 104) AS test
FROM [DATAWAREHOUSE].[dbo].[blabla];
But to find the problem, you can use:
SELECT policyeffective_date
FROM [DATAWAREHOUSE].[dbo].[blabla]
WHERE try_convert(datetime, policyeffective_date, 104) IS NULL AND
policyeffective_date IS NOT NULL
This works for me in SQL Server 2017:
select cast(cast(20190430 as nvarchar) as DATE) AS DateResult
Output:
DateResult
2019-04-30

How to get alone time 11:21 (highlighted in red) with SQL Server database? [duplicate]

This question already has answers here:
SQL Server 2005 DateTime (return only hh:mm)
(2 answers)
Closed 6 years ago.
Done automatically add the date to the time of the database SQL Server 2005 Express. So when you add a new record in the database is added automatically date and time in the form of:
Create Table Baza_test
(
ID bigint IDENTITY (1,1) NOT NULL,
Client nvarchar (23)
address nvarchar (46)
DateOfAddmission datetime default CURRENT_TIMESTAMP
aaaaaadres nvarchar (46)
);
How to get alone time (highlighted in red):
with SQL Server database?
With the following query:
select CONVERT(VARCHAR(5), DateOfAddmission,108) AS [time] from Baza_test where ID = 1;
There is already similar question out there, please refer to the link bellow:
https://stackoverflow.com/a/7710495/4558361 by t-clausen.dk
The CONVERT(VARCHAR) answers are valid, if you want more flexibility and don't know the conversion codes like me :), you could do it with FORMAT:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT( #d, 'hh:mm') AS 'Time Result'
If 2012+
Select format(GetDate(),'HH:mm') --For the 24 clock
Select format(GetDate(),'hh:mm tt') --For the 12 clock
That said, I was informed that FORMAT() has a poor performance
Try like this,
12 HOURS FORMAT
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), DateOfAddmission, 100), 7)) FROM Baza_test
24 HOURS FORMAT
SELECT CONVERT(VARCHAR(5),DateOfAddmission, 108) FROM Baza_test
Apart from the answer given here another way is this -
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))
Output
05:48
And in your case its -
SELECT CAST(CAST(DateOfAddmission AS Time(0)) AS VARCHAR(5)) AS [time]
FROM Baza_test
WHERE ID = 1;

sql server query current date from database [duplicate]

This question already has answers here:
how to get current datetime in SQL?
(9 answers)
Closed 7 years ago.
I want to find few data from SQL Server that only query by current date. The purpose is to view today transaction only
Here is my code
SELECT * FROM dbo.Student.studentProfile
WHERE TransactionDate = curdate()
RESULT
Curdate is not a recongined built-in function name
If you are looking for the current date:
WHERE TransactionDate = cast(getdate() as date)
Or if you prefer ANSI standards:
WHERE TransactionDate = cast(CURRENT_TIMESTAMP as date)
Different sql implementations (ie SQL Server, Mysql, postgresql etc) can have different methods supported. For SQL Server the method you want to use is GETDATE() instead of CURDATE()
The documentation for this method is here: https://msdn.microsoft.com/en-us/library/ms188383.aspx
Try this.
select * from dbo.Student.studentProfile
WHERE TransactionDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND TransactionDate < dateadd(day,datediff(day,0,GETDATE()),0)

How to cast GETDATE() to be only time ignore day,month and year [duplicate]

This question already has answers here:
Compare time part of DateTime data type in SQL Server 2005
(6 answers)
Closed 4 years ago.
I have a table for store takt-time. It's compound with "day_shift(1)" and "night_shift(0)" like below.
If I put current time(by using GETDATE() function ).
I need to know this current time is "day(1)" or "night(0)". my query like this.
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE GETDATE() BETWEEN [start_time] AND[end_time]
But it can't found because a date received from GETDATE() isn't '1900-01-01'
How to compare a only time and ignore day,month,year.
What should I do?
You can cast a DateTime as a Time data type. For example:
SELECT
[day_shift_flag]
FROM [company_working_time]
WHERE
CAST(GETDATE() AS TIME) BETWEEN CAST([start_time] AS TIME) AND CAST([end_time] AS TIME)
By Following Way You can Ignore the day,month,year
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE DatePart(HH,GETDATE()) BETWEEN DatePart(HH,[start_time]) AND DatePart(HH,[end_time])

Simple DateTime sql query

How do I query DateTime database field within a certain range?
I am using SQL SERVER 2005
Error code below
SELECT *
FROM TABLENAME
WHERE DateTime >= 12/04/2011 12:00:00 AM
AND DateTime <= 25/05/2011 3:53:04 AM
Note that I need to get rows within a certain time range. Example, 10 mins time range.
Currently SQL return with Incorrect syntax near '12'."
You missed single quote sign:
SELECT *
FROM TABLENAME
WHERE DateTime >= '12/04/2011 12:00:00 AM' AND DateTime <= '25/05/2011 3:53:04 AM'
Also, it is recommended to use ISO8601 format YYYY-MM-DDThh:mm:ss.nnn[ Z ], as this one will not depend on your server's local culture.
SELECT *
FROM TABLENAME
WHERE
DateTime >= '2011-04-12T00:00:00.000' AND
DateTime <= '2011-05-25T03:53:04.000'
You need quotes around the string you're trying to pass off as a date, and you can also use BETWEEN here:
SELECT *
FROM TABLENAME
WHERE DateTime BETWEEN '04/12/2011 12:00:00 AM' AND '05/25/2011 3:53:04 AM'
See answer to the following question for examples on how to explicitly convert strings to dates while specifying the format:
Sql Server string to date conversion
This has worked for me in both SQL Server 2005 and 2008:
SELECT * from TABLE
WHERE FIELDNAME > {ts '2013-02-01 15:00:00.001'}
AND FIELDNAME < {ts '2013-08-05 00:00:00.000'}
You can execute below code
SELECT Time FROM [TableName] where DATEPART(YYYY,[Time])='2018' and DATEPART(MM,[Time])='06' and DATEPART(DD,[Time])='14
SELECT *
FROM TABLENAME
WHERE [DateTime] >= '2011-04-12 12:00:00 AM'
AND [DateTime] <= '2011-05-25 3:35:04 AM'
If this doesn't work, please script out your table and post it here. this will help us get you the correct answer quickly.
select getdate()
O/P
----
2011-05-25 17:29:44.763
select convert(varchar(30),getdate(),131) >= '12/04/2011 12:00:00 AM'
O/P
---
22/06/1432 5:29:44:763PM
Others have already said that date literals in SQL Server require being surrounded with single quotes, but I wanted to add that you can solve your month/day mixup problem two ways (that is, the problem where 25 is seen as the month and 5 the day) :
Use an explicit Convert(datetime, 'datevalue', style) where style is one of the numeric style codes, see Cast and Convert. The style parameter isn't just for converting dates to strings but also for determining how strings are parsed to dates.
Use a region-independent format for dates stored as strings. The one I use is 'yyyymmdd hh:mm:ss', or consider ISO format, yyyy-mm-ddThh:mi:ss.mmm. Based on experimentation, there are NO other language-invariant format string. (Though I think you can include time zone at the end, see the above link).
if you have a type of datetime and you want to check between dates only ,,,use cast to select between two dates ....
example...
... where cast( Datetime as date) >= cast( Datetime as date) AND cast( Datetime as date) <= cast( Datetime as date)