-1
declare @Date date
SET   @Date =GETDATE()
print @date

Declaration gives me the result : 2013-08-04

But I need also milliseconds and minutes.

How I can achieve this?

Marcello Miorelli
  • 3,368
  • 4
  • 44
  • 67
sakir
  • 3,391
  • 8
  • 34
  • 50
  • 5
    Well, ***you*** defined a variable of type `DATE` - that's **just the date** - ***no time***! Use `@Date DATETIME2(3)` and you'll get hours, minutes, seconds, and milliseconds.... all the datatypes are ***well documented*** in the relevant [SQL Server Books Online](http://msdn.microsoft.com/en-us/library/ms187752.aspx) pages - use them! That's what they're there for! – marc_s Aug 04 '13 at 08:43

2 Answers2

8

Declare as datetime2 rather than date:

declare @Date datetime2
            --^^^^^^^^^
SET   @Date = SYSDATETIME()
print @date

Ref.: Data Types (Transact-SQL)

Updated: Replaced GETDATE() with SYSDATETIME() as mentioned by @Martin Smith

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 5
    There is nothing 'wrong' with date. But all a variable of type date can store is Date! – Mitch Wheat Aug 04 '13 at 08:43
  • 4
    @user2460637: It's a *date*... not a date and time. See http://technet.microsoft.com/en-us/library/bb630352.aspx – Jon Skeet Aug 04 '13 at 08:43
  • 2
    @user2460637 Be aware that there is probably a resolution limit on GETDATE(). On my computer I can't get two different times nearer than 3 MS – xanatos Aug 04 '13 at 08:55
  • 2
    @xanatos - Yes. `GETDATE()` returns `datetime` datatype which has 300 ticks per second. `SYSDATETIME()` is the newer alternative that returns `datetime2` – Martin Smith Aug 04 '13 at 09:57
0

getdate() has minutes and even millisenconds in it. you can see the different styles here.

if you run this query:

DECLARE @now datetime
SET @now = GETDATE()
select convert(nvarchar(50), @now, 0) as output, 0 as style 
union select convert(nvarchar(50), @now, 1), 1
union select convert(nvarchar(50), @now, 2), 2
union select convert(nvarchar(50), @now, 3), 3
union select convert(nvarchar(50), @now, 4), 4
union select convert(nvarchar(50), @now, 5), 5
union select convert(nvarchar(50), @now, 6), 6
union select convert(nvarchar(50), @now, 7), 7
union select convert(nvarchar(50), @now, 8), 8
union select convert(nvarchar(50), @now, 9), 9
union select convert(nvarchar(50), @now, 10), 10
union select convert(nvarchar(50), @now, 11), 11
union select convert(nvarchar(50), @now, 12), 12
union select convert(nvarchar(50), @now, 13), 13
union select convert(nvarchar(50), @now, 14), 14
--15 to 19 not valid
union select convert(nvarchar(50), @now, 20), 20
union select convert(nvarchar(50), @now, 21), 21
union select convert(nvarchar(50), @now, 22), 22
union select convert(nvarchar(50), @now, 23), 23
union select convert(nvarchar(50), @now, 24), 24
union select convert(nvarchar(50), @now, 25), 25
--26 to 99 not valid
union select convert(nvarchar(50), @now, 100), 100
union select convert(nvarchar(50), @now, 101), 101
union select convert(nvarchar(50), @now, 102), 102
union select convert(nvarchar(50), @now, 103), 103
union select convert(nvarchar(50), @now, 104), 104
union select convert(nvarchar(50), @now, 105), 105
union select convert(nvarchar(50), @now, 106), 106
union select convert(nvarchar(50), @now, 107), 107
union select convert(nvarchar(50), @now, 108), 108
union select convert(nvarchar(50), @now, 109), 109
union select convert(nvarchar(50), @now, 110), 110
union select convert(nvarchar(50), @now, 111), 111
union select convert(nvarchar(50), @now, 112), 112
union select convert(nvarchar(50), @now, 113), 113
union select convert(nvarchar(50), @now, 114), 114
union select convert(nvarchar(50), @now, 120), 120
union select convert(nvarchar(50), @now, 121), 121
--122 to 125 not valid
union select convert(nvarchar(50), @now, 126), 126
union select convert(nvarchar(50), @now, 127), 127
--128, 129 not valid
union select convert(nvarchar(50), @now, 130), 130
union select convert(nvarchar(50), @now, 131), 131
--132 not valid
order BY style

you get these results - 42 in total:

enter image description here

the datetime time is shown in each of the styles from the query above

Have a look at style 109. it shows minutes and millisenconds as well as the date and time.

if I run the following:

print 'the date and time now is: ' + convert(nvarchar(50), getdate(),109)

I get exactly that - printed!

enter image description here

Marcello Miorelli
  • 3,368
  • 4
  • 44
  • 67