Current System Time - SQL - sql

How to get the current system time only in SQL Server / any database.
The date is not required...only the current time is required.

You can use getdate() or current_timestamp
This will give you both date and time but then you can format it however you need.
If you are using SQL Server 2008+ you can even format the result as time:
select cast(getdate() as time)
If you are not in SQL Server 2008+, then you can use many different methods to get the time only including:
SELECT convert(char(8), getdate(), 108)
See SQL fiddle demo with several versions

Using `getdate()` like:
select getdate()
=============================
and get only time use convert function with 108 code like :
select convert(varchar,GETDATE(),108)

select CONVERT(VARCHAR(8),GETDATE(),108) CurrTime;

There is 3 different statement available to get the current system time
select CONVERT(VARCHAR(8),getDate(),108)
select CONVERT(VARCHAR(8),{fn NOW()},108)
select CONVERT(VARCHAR(8),CURRENT_TIMESTAMP,108)
They all produce the same results and performance are the same, therefore it is really whatever one of the three you prefer the look of.

Related

Sql Server Table date query showing incorrect result

I have a Sql server table which contains below Date values(4th october)
Now Below query is not showing any result
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/04/2018' which is not correct.
But If I write
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/05/2018' it is returning me all results.
What I am doing wrong.
There are two problems with this query. The first, is that it's using a localized string. To me, it looks like it's asking for rows between January and April. The unambiguous date format is YYYYMMDD. YYYY-MM-DD by itself may not work in SQL server as it's still affected by the language. The ODBC date literal, {d'YYYY-MM-DD'} also works unambiguously.
Second, the date parameters have no time which defaults to 00:00. The stored dates though have a time element which means they are outside the search range, even if the date parameter was recognized.
The query should change to :
select
*
from [dbo].[TB_AUDIT] TBA
where
cast(TBA.ActionDate as date) between '20181001' and '20181004'
or
cast(TBA.ActionDate as date) between {d'2018-10-01'} and {d'2018-10-04'}
Normally, applying a function to a field prevents the server from using any indexes. SQL Server is smart enough though to convert this to a query that covers the entire date, essentially similar to
where
TBA.ActionDate >='2018:10:01T00:00' and TBA.ActionDate <'2018-10-05T00:00:00'
When you don't specify a time component for a DATETIME, SQL Server defaults it to midnight. So in your first query, you're asking for all results <='2018-10-04T00:00:00.000'. All of the data points in your table are greater than '2018-10-04T00:00:00.000', so nothing is returned.
You want
TBA.ActionDate >= '2018-10-01T00:00:00.000' and TBA.ActionDate < '2018-10-05T00:00:00.000'`
Use properly formatted dates!
select *
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '2018-10-01' and TBA.ActionDate <= '2018-10-04'
YYYY-MM-DD isn't just a good idea. It is the ISO standard for date formats, recognized by most databases.
when you just filter by the date, it is with regard to the time as per the standard.

SQL Server 2008 query check date on same day

I've written a query to check whether more than one record exists on the same day. Currently the excerpt from my query that performs the restriction looks like this :
GROUP BY
entry_date
HAVING
COUNT(entry_date) > 1
As the entry date column is defined as a datetime, does it check against the full datetime or just the date?
Thanks.
if entry_date is DATETIME ,your group by wont work as expected.you need to CAST it to DATE.Cast(Datetime)to date is sargable as well.
GROUP BY cast(entry_date as DATE)
having count(cast(entry_date as DATE)) > 1
Since you don't cast or convert it to anything else, naturally it uses all data available. So it would group data together with the exact same datetime. Why would you expect anything else?
It should be full datetime.
CONVERT(date, entry_date)
should separate out the date.

SQL Server dates

I use Classic ASP and SQL Server 2012. I have a program that inputs into the database using now(). Originally it was formnatdatetime(now(),2).
For the majority of time everything was fine but for some reason (which is why I'm asking) occasionally it would put the date in the database in the wrong format. So instead of ddmmyyyy it would be mmddyyyy.
I cannot see how or why when the code is the same, the database is the same. I assume now() or getdate() in TSQL is server specific.
You could use the format function
SELECT FORMAT(GETDATE(),'ddMMyyyy')
Results:
11042015
MS SQL Server 2012 does not know NOW() function! As Gordon Linoff mentioned, you need to use GETDATE() instead!
I'd suggest to read this: Date and Time Data Types and Functions (Transact-SQL) together with: CAST and CONVERT (Transact-SQL)
SELECT CONVERT(VARCHAR(30), GETDATE(), 103) AS CustomDateFormat
-- returns: dd/mm/yyyy
Note that sometimes, sql server can't convert date stored as a string, so you need to use SET DATEFORMAT, especially when sql server uses different date format:
SET DATEFORMAT dmy;
SELECT CONVERT(VARCHAR(30), '11/04/2015', 121) AS NewDateTime

How to apply a particular format of date of birth in a SQL query?

Suppose I want a particular format of date of birth, like (mm-dd-yyyy).
How can I do that in SQL?
Although some people have listed the proper syntax for this in multiple RDBMSs (you really need to indicate which one you're using), I'd like to point out that formatting your data is typically something that should be done by the front end of your application. That's not to say that there's never a reason to do formatting in SQL, but usually it's best to just pass it as a date/time to the front end and let the front end handle how it will represent it to the user. Hopefully, you understand the difference between a date/time and a string.
Use CONVERT function if it is SQL SERVER
Select CONVERT(VARCHAR(10),Birthdate,110) from YourTable
Assuming your RDBMS is SQL Server, you can do the following:
SELECT CONVERT(VARCHAR(10), [DateOfBirth], 110)
There's more information about date formatting here.
SQL Server
SELECT convert(varchar, getdate(), 110)
SELECT convert(varchar, DateOfBirth, 110) FROM YourTable
How to format datetime & date in Sql Server 2005
If you're using Oracle:
select to_char(your_date_column,'MM-DD-YYYY') from your_date_table;

Sql Shorthand For Dates

Is there a way to write a query equivalent to
select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';
but where you could specify only 1 date and say you want the results for that entire day until the next one.
I'm just making this up, but something of the form:
select * from log_table where dt = 'nov-27-2009':+1;
I do not believe there is one method that is portable to all RDBMSes.
A check in one of my references (SQL Cookbook) shows that no one RDBMS solves the problem quite the same way. I would recommend checking out Chapter 8 of that book, which covers all of the different methods for DB2, Oracle, PostgreSQL, MySQL.
I've had to deal with this issue in SQLite, though, and SQL Cookbook doesn't address that RDBMS, so I'll mention a bit about it here. SQLite doesn't have a date/time data type; you have to create your own by storing all date/time data as TEXT and ensure that your application enforces its formatting. SQLite does have a set of date/time conversion functions that allow you to add nominal date/times while maintaining the data as strings. If you need to add two time durations (HH:MM:SS) to each other, though, based upon data that you've stored in text columns that you are treating as date/time data, you'll have to write your own functions (search for "Defining SQLite User Functions") and attach them to the database at runtime via a call to sqlite3_create_function(). If you want an example of some user functions that add time values, let me know.
For MS SQL Server, check out DATEPART.
/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');
With SQL Server, you could
Select * From table
Where dt >= DateAdd(day, DateDiff(day, 0, #ParamDate), 0)
And dt < DateAdd(day, DateDiff(day, 0, #ParamDate), 1)
As long as you are dealing with the date data type for the respective data type, the following will work:
t.date_column + 1
...will add one day to the given date. But I have yet to find a db that allows for implicit data type conversion into a date.
SELECT '12-10-2009' + 1
...will fail on SQL Server because SQL Server only performs the implicit conversion when comparing to a datetime data type column. So you need to use:
SELECT CONVERT(DATETIME, '12-10-2009') + 1
For Oracle, you'd have to use the TO_DATE function; MySQL would use something like STR_TO_DATE, etc.
Have a column that just has the date part (time is 00:00:00.000) and then you can add a where clause: WHERE dt = '2009-11-27'