Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 35494
  • Last Modified:

Using GETDATE() to return a date only without time

I need to use (I think) the GETDATE() function to return the current date, but I need to make sure that it is only the date and no time, or at least time to be 0.
Is this the right way to go about it

CONVERT(DateTime, CONVERT(Char, GETDATE(), 103), 103))

or is there a better way

Thanks in advance
0
mikelittlewood
Asked:
mikelittlewood
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Thandava VallepalliCommented:
yours is best one....

itsvtk
0
 
Thandava VallepalliCommented:
this is enough right?,  why are u again converting i.e 2 times converting in ur query

select convert(varchar, getdate(), 103)

0
 
mikelittlewoodAuthor Commented:
Im converting it twice so I get it back in date form of the format 'dd/mm/yyyy 00:00:00' and not something like 'dd/mm/yyyy 12:30:05'
It seems to set the time to 0 which is what I want
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
BillAn1Commented:
you can get slight improvement if instead of converting to a char, you convert to integer this will truncate the time portion. If you want you can cast back to datetime, but this will be done explicitly for you anyway if you assign to a datetime variable, or complare to a datetime field etc.

select cast(cast(getdate() as integer) as datetime)
0
 
anthonywjones66Commented:
This is better

CONVERT(datetime, FLOOR(CONVERT(float(24), GETDATE())) )

casting direct to int rounds to the nearest integer which is not what you want

Anthony
0
 
mikelittlewoodAuthor Commented:
Thanks for the advice all
0
 
BillAn1Commented:
absolutely right, sorry for the bum steer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now