[code language="sql"]
DECLARE @startTime Time = '01:00:00'
DECLARE @endTime TIME = '02:30:00'
-- Get the number of seconds between these two times
-- (eg. there are 5400 seconds between 1 AM and 2.30 AM)
DECLARE @maxSeconds int = DATEDIFF(ss, @startTime, @endTime)
-- Get a random number of seconds between 0 and the number of
-- seconds between @startTime and @endTime (@maxSeconds)
DECLARE @randomSeconds int = (@maxSeconds + 1)
* RAND(convert(varbinary, newId() ))
-- Add the random number of seconds to @startTime and return that random time of day
SELECT (convert(Time, DateAdd(second, @randomSeconds, @startTime)))
AS RandomTime
--Note: Above code cannot be used inside the function as it would give following errors
--Invalid use of a side-effecting operator 'newid' within a function.
--Invalid use of a side-effecting operator 'rand' within a function.
[/code]
Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts
Monday, April 1, 2013
Sunday, April 29, 2012
Working With Excel: Tips and Tricks
Replace Spaces with other characters : Use Substitute function
=SUBSTITUTE(G2," ","")
=SUBSTITUTE(G2," ","")
Labels:
excel,
microsoft,
tips,
tricks,
working with excel
Monday, March 19, 2012
Create Comma Separated List- Sql tips and tricks
Create list of Integer type column
[code]
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + CAST(ColumnName AS Varchar(10))
FROM TableName
SELECT @listStr
GO
[/code]
Replace ColumnName and TableName
[code]
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + CAST(ColumnName AS Varchar(10))
FROM TableName
SELECT @listStr
GO
[/code]
Replace ColumnName and TableName
Labels:
create list,
microsoft,
Sql Server,
tips,
tricks
Subscribe to:
Posts (Atom)