SQL Query - Have to determine if it is a specific day of the month (ex: 2 Tuesday of month) - sql

I have an app that allows users schedule an action to occur in the future. For example, that can select a date and schedule it to run on that day every month (ex: the 15th of each month). However, I now need to allow them to select a week day and week of the month. For example, they need to run an action the first friday of the month. Therefore I am allowing the to select the weekday (monday, tuesday, wednesday....) and week of the month (1st, 2nd, 3rd, 4th or 5th).
Here is the query I currently use:
Declare #nowString varchar(19)
Declare #nowDateString varchar(19)
Declare #now datetime
Declare #lastMinute datetime
Declare #nowDate datetime
Set #nowString = '#currentDateTime#:00'
Set #nowDateString = LEFT(#nowString, 10)
set #now = #nowString
set #nowDate = DATEADD(dd, 0, DATEDIFF(dd, 0, #now))
set #lastMinute = DATEADD(mi, -1, #now)
select *
from message_prepared
where schedule = '1'
and active = '1'
and noaa = '0'
and (
(
schedule_type = 'recurring'
and startdate <= #nowDate
and isnull(enddate, DATEADD(yy, 1, #nowDate)) >= #nowDate
and (
#nowDateString + ' ' + isnull(recurring_start_time_hour, '00') + ':' + isnull(recurring_start_time_min, '00') + ':00' = #now
or #nowDateString + ' ' + isnull(recurring_start_time_hour, '00') + ':' + isnull(recurring_start_time_min, '00') + ':00' = #lastMinute
)
-- Test for different type of recurring
and (
( ltrim(rtrim(recurring)) = 'M' and DATEPART(dd, startdate) = DATEPART(dd, #now) )
or ( ltrim(rtrim(recurring)) = 'W' and DATEPART(dw, startdate) = DATEPART(dw, #now) )
or ltrim(rtrim(recurring)) = 'D'
)
)
or (
schedule_type = 'once'
and startdate = #nowDate
and (
#nowDateString + ' ' + onetime_start_time_hour + ':' + onetime_start_time_min + ':00' = #now
or #nowDateString + ' ' + onetime_start_time_hour + ':' + onetime_start_time_min + ':00' = #lastMinute
)
)
)
and repeat_multiple_times = 0
UNION ALL
SELECT *
FROM MESSAGE_PREPARED
WHERE schedule = '1'
AND active = 1
AND noaa = 0
AND recurring = 'D'
AND repeat_multiple_times = 1
AND startDate IS NOT NULL
AND recurring_start_time_hour IS NOT NULL
AND recurring_start_time_hour < 24
AND recurring_start_time_min IS NOT NULL
AND recurring_start_time_min < 60
AND startdate <= #nowDate
AND ISNULL(enddate, DATEADD(yy, 1, #nowDate)) >= #nowDate
AND
(
CASE WHEN repeat_unit = 'M'
THEN
DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) % repeat_interval
ELSE
DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) % (repeat_interval * 60)
END = 0
OR
CASE WHEN repeat_unit = 'M'
THEN
(DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) - 1) % repeat_interval
ELSE
(DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) - 1) % (repeat_interval * 60)
END = 0
)
This will only occur when reocurring is set to "M" and I would like to determine if today is the specific day of the week, week of the month and hour/min.

This is pretty simple logic. Today is the nth DOW of the month when the following is true:
Today is that day of the week
The day of the month is between 7*(n-1) + 1 and 7 * n
So, the first Monday of the month is always between the 1st and 7th, and so on. Here is an example case statement to test this:
declare #DayOfWeek varchar(255) = 'Thursday';
declare #Which int = 3;
select (case when datename(dw, Today) = #DayOfWeek and
(DAY(Today) - 1) / 7 = #Which - 1
then 1
else 0
end)
from (select CAST(getdate() as date) as Today) t
I've structured the query this way so you can test it with different values. Just replace the expression that defines Today, with something like getdate() - 3 or '2013-01-01'.

Related

How do you update Year field in the datetime column using another datetime column?

I have two columns (Birth_date and Purchase_date) and would like to create a column (Birth_date_update) using following conditions
Conditions:
When Birth_date is later than Purchase_date, take birth_date.
When Birth_date is earlier than Purchase_date, Birth_date_update will take the year from purchase_date and override year from birth_date.
If the final value from condition 2 is still earlier than purchase_date, than find next birth_date anniversary that is bigger than purchase date.
Note: Condition 2 and 3 can be summarized as find earliest anniversary of birth_date greater than purchase_date.
Birth_date purchase_date Birth_date_update
2002-02-21 2006-05-11 2007-02-21
2004-01-18 2004-01-25 2005-01-18
2011-07-24 2011-04-09 2011-07-24
2006-12-16 2007-10-08 2007-12-16
2007-04-30 2008-03-14 2008-04-30
I have no idea how to code this.. please help!
update my_table
set birth_date_update = case
when birth_date > purchase_date then birth_date -- Condition 1
when birth_date < purchase_date then dateadd(year, datediff(year, birth_date, purchase_date), birth_date) -- Condition 2
when dateadd(year, datediff(year, birth_date, purchase_date), purchase_date) < purchase_date
then dateadd(year, 1, dateadd(year, datediff(year, birth_date, purchase_date), purchase_date)) end -- Condition 3
Result from above answer doesn't seem to be correct. Looks like Condition 3 is not working consistent. So I added my answer
create table TAB1 (BD date, PD date, BDU date)
insert into TAB1 values
('2002-02-21', '2006-05-11', NULL),
('2004-01-18', '2004-01-25', NULL),
('2011-07-24', '2011-04-09', NULL),
('2006-12-16', '2007-10-08', NULL),
('2007-04-30', '2008-03-14', NULL)
;with cc as (
select BD, PD,
case
when BD > PD then BD
when BD < PD then cast(cast(year(PD) as varchar) + '-' + cast(month(BD) as varchar) + '-' + cast(day(BD) as varchar) as date)
end as BDU
from TAB1
)
, cc2 as (
select BD, PD,
case
when BDU < PD then dateadd(day, 365, BDU)
else BDU
end as BDU
from cc
)
update T1
set BDU = cc2.BDU
from TAB1 T1
inner join cc2 on cc2.BD = T1.BD and cc2.PD = T1.PD
select * from TAB1
I do not want to be left out of this one. Here is my answer
UPDATE dbo.BirthDateTable SET Birth_date_update =
CASE WHEN Birth_date >= purchase_date THEN Birth_date -- Condition 1
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) -- Condition 2
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)))) -- Condition 3
END
END
Here is a select example.
SELECT
CASE WHEN Birth_date >= purchase_date THEN Birth_date -- Condition 1
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) -- Condition 2
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)))) -- Condition 3
END
END
FROM dbo.BirthDateTable
Other post about above answer not being correct was written when the order of the answers was different on the page. I believe this answer meets the criteria.#JamieHong, could you see if this select works on your system? I padded the month and day with zeros.
SELECT
CASE WHEN Birth_date >= purchase_date THEN Birth_date
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2)) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2))
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2)))
END
END AS CalcDate
FROM dbo.BirthDateTable

How to get First and Last for every month

How to Add First for 1 to 10 dates for a month and Add Last for 20 to 30 days of month.
DECLARE #Today DATE = '2016-11-05'
Select CONVERT(VARCHAR(5),datepart (DW, #Today)-1 )+' DAYS of '+ LEFT(DATENAME(month,#Today),3) Comments
I'm getting like this
Comments
6 DAYS of Nov
How to get like this :
Comments
First 6 DAYS of Nov
if I give date as '2016-11-24'
need output like this
Comments
Last 4 DAYS of Nov
Suggest me the way to proceed
Use a case statement:
Select (CASE WHEN day(#today) <= 10 THEN 'First '
WHEN day(#today) >= 20 THEN 'Last '
ELSE ''
END) + CONVERT(VARCHAR(5), datepart(DW, #Today)-1 ) + ' DAYS of ' +
LEFT(DATENAME(month, #Today), 3) as Comments
EDIT:
Oh, now I see the original query was not right. So you want something more like this:
Select (CASE WHEN day(#today) <= 10 THEN 'First ' + DATENAME(day, #today) + ' DAYS of ' + LEFT(DATENAME(month, #Today), 3)
WHEN day(#today) >= 20 AND MONTH(#Today) IN (1, 3, 5, 7, 8, 10, 12) THEN 'Last ' + CAST(31 - day(#today) as varchar(255))
WHEN day(#today) >= 20 AND MONTH(#Today) IN (4, 6, 9, 11) THEN 'Last ' + CAST(30 - day(#today) as varchar(255))
WHEN day(#today) >= 20 AND MONTH(#Today) IN (2) AND YEAR(#Today) % 4 = 0 THEN 'Last ' + CAST(29 - day(#today) as varchar(255))
WHEN day(#today) >= 20 AND MONTH(#Today) IN (2) AND YEAR(#Today) % 4 <> 0 THEN 'Last ' + CAST(29 - day(#today) as varchar(255))
ELSE CAST(day(#today) as varchar(255))
END) + ' DAYS of ' + LEFT(DATENAME(month, #Today), 3) as Comments
DECLARE #Today DATE = '2016-11-09'
Select (CASE WHEN day(#today) <= 10 THEN 'First ' + DATENAME(day, #today)
WHEN day(#today) >= 20 THEN 'Last ' + CAST(DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#Today)+1,0))) - day(#today) as varchar(255))
ELSE CAST(day(#today) as varchar(255) )
END) + ' DAYS of ' + LEFT(DATENAME(month, #Today), 3)
With SQL Server 2012 developers can now use new SQL date function EOMonth() for calculating the last date of the month where a given date is in.
Here is my query
--DECLARE #Today DATE = '2016-11-05'
DECLARE #Today DATE = '2016-11-24'
SELECT
case when DATEPART(dd,#Today) <= 10 then
'First ' + convert(varchar(2), DATEPART(dd,#Today)) + ' of ' + DATENAME(mm,#Today)
else
'Last ' + convert(varchar(2), DATEDIFF(dd, #Today, EOMONTH (#Today))) + ' of ' + DATENAME(mm,#Today)
end
The SQL EOMonth() function makes it easier to calculate the last date of a month, and also indirectly calculating the first date of next month or previous month
SELECT case when DATEPART(dd,#Today) <= 10 then
'First ' + convert(varchar(2), DATEPART(dd,#Today)) + ' DAYS of ' + DATENAME(mm,#Today)
WHEN DATEPART(dd,#Today) >= 20 then 'Last ' + convert(varchar(2),
DATEDIFF(dd, #Today, EOMONTH (#Today))) + ' DAYS of ' + DATENAME(mm,#Today)
ELSE #Today END

Get day of the week in month (2nd Tuesday, etc.)

I need an algorithm for calculating the number of the day of the week in the month. Like 1st Friday of the month, 3rd Monday of the month, etc.)
Any ideas are appreciated.
Here is the final result:
declare #dt date = GetDate()
declare #DayOfWeek tinyint = datepart(weekday,#dt)
declare #DayOfMonth smallint = day(#dt)
declare #FirstDayOfMonth date = dateadd(month,datediff(month,0,#dt),0)
declare #DayOfWeekInMonth tinyint = #DayOfMonth / 7 + 1 -
(case when day(#FirstDayOfMonth) > day(#dt) then 1 else 0 end)
declare #Suffix varchar(2) =
case
when #DayOfWeekInMonth = 1 then 'st'
when #DayOfWeekInMonth = 2 then 'nd'
when #DayOfWeekInMonth = 3 then 'rd'
when #DayOfWeekInMonth > 3 then 'th'
end
select
cast(#DayOfWeekInMonth as varchar(2))
+ #Suffix
+ ' '
+ datename(weekday,#Dt)
+ ' of '
+ datename(month,#dt)
+ ', '
+ datename(year,#Dt)
PS: And if you can think of a better way to state the problem, please do.
Followint code will give you 1st Wednesday of April 2014 for today:
SELECT cast((DATEPART(d, GETDATE() - 1) / 7) + 1 as varchar(12))
+ 'st ' + DATENAME(WEEKDAY, getdate()) + ' of ' +
DATENAME(month, getdate()) + ' ' + DATENAME(year, getdate());
For any date use the code below. It gives 5th Tuesday of April 2014 for #mydate = '2014-04-29' in the example:
DECLARE #mydate DATETIME;
SET #mydate = '2014-04-29';
SELECT
case
when DATEPART(d, #mydate) = 1 then cast((DATEPART(d, #mydate ) / 7) + 1 as varchar(12))
else cast((DATEPART(d, #mydate - 1) / 7) + 1 as varchar(12))
end
+
case
when (DATEPART(d, #mydate - 1) / 7) + 1 = 1 then 'st '
when (DATEPART(d, #mydate - 1) / 7) + 1 = 2 then 'nd '
when (DATEPART(d, #mydate - 1) / 7) + 1 = 3 then 'rd '
else 'th '
end
+ DATENAME(WEEKDAY, #mydate) + ' of ' +
DATENAME(month, #mydate) + ' ' + DATENAME(year, #mydate) as [Long Date Name]
Okeeeey my tuuuurn ,
Please rate my answer Metaphor hhh, Here's the cooode :
declare #v_month nvarchar(2) = '04'
,#v_annee nvarchar(4) = '2014'
declare #v_date date = convert(date,#v_annee+'-'+#v_month+'-01')
declare #v_date_2 date = dateadd(M,1,#v_date)
if OBJECT_ID('temp') is not null
drop table temp
create table temp(_date date, _DayOfMonth nvarchar(20), _order int)
while (#v_date<#v_date_2)
begin
set #v_date =#v_date;
WITH _DayOfWeek AS (
SELECT 1 id, 'monday' Name UNION ALL
SELECT 2 id, 'tuesday' Name UNION ALL
SELECT 3 id, 'wednesday' Name UNION ALL
SELECT 4 id, 'thursday' Name UNION ALL
SELECT 5 id, 'friday' Name UNION ALL
SELECT 6 id, 'saturday' Name UNION ALL
SELECT 7 id, 'sunday' Name)
insert into temp(_date,_DayOfMonth)
SELECT
#v_date
,(select Name from _DayOfWeek where id = DATEPART(WEEKDAY,#v_date))
SET #v_date = DATEADD(DAY,1,#v_date)
END
UPDATE tmp1
SET _order = _order_2
FROM temp tmp1
INNER JOIN
(SELECT *, ROW_NUMBER() OVER(PARTITION BY _DayOfMonth ORDER BY _date ASC) AS _order_2 FROM temp) tmp2
ON tmp1._date = tmp2._date
SELECT * FROM temp
SELECT *
FROM temp
WHERE _DayOfMonth = 'thursday'
AND _order = 3
I hope this will help you :)
Good Luck
OK, here's what I came up with, I'll +1 everyone who answered anyway:
declare #dt date = GetDate()
declare #DayOfWeek tinyint = datepart(weekday,#dt)
declare #DayOfMonth smallint = day(#dt)
declare #FirstDayOfMonth date = dateadd(month,datediff(month,0,#dt),0)
declare #DayOfWeekInMonth tinyint =
#DayOfMonth / 7 + 1
- (case when day(#FirstDayOfMonth) > day(#dt) then 1 else 0 end)
declare #Suffix varchar(2) =
case
when #DayOfWeekInMonth = 1 then 'st'
when #DayOfWeekInMonth = 2 then 'nd'
when #DayOfWeekInMonth = 3 then 'rd'
when #DayOfWeekInMonth > 3 then 'th'
end
select
cast(#DayOfWeekInMonth as varchar(2))
+ #Suffix
+ ' '
+ datename(weekday,#Dt)
+ ' of '
+ datename(month,#dt)
+ ', '
+ datename(year,#Dt)
declare #dt date = getdate()
declare #DayOfMonth smallint = datepart(d, #dt)
declare #Suffix varchar(2) =
case
when floor((#DayOfMonth - 1) / 7.0) = 0 then 'st' -- implies there were no such days previously in the month
when floor((#DayOfMonth - 1) / 7.0) = 1 then 'nd'
when floor((#DayOfMonth - 1) / 7.0) = 2 then 'rd'
else 'th'
end
select cast(floor((#DayOfMonth - 1) / 7.0) + 1 as varchar(1)) + #Suffix +
' ' + datename(weekday, #dt) + ' of ' + datename(month, #dt) +
', ' + datename(year, #dt)
DECLARE #dt DATETIME
SET #dt = DATEADD(d, 6, GETDATE())
SELECT #dt,
CAST((DAY(#dt) / 7) + CASE WHEN DATEPART(weekday, #dt) >= DATEPART(weekday, CAST(MONTH(#dt) AS NVARCHAR) + '/01/' + CAST(YEAR(#dt) AS NVARCHAR)) THEN 1 ELSE 0 END AS NVARCHAR)
+ '' + CASE (DAY(#dt) / 7) + CASE WHEN DATEPART(weekday, #dt) >= DATEPART(weekday, CAST(MONTH(#dt) AS NVARCHAR) + '/01/' + CAST(YEAR(#dt) AS NVARCHAR)) THEN 1 ELSE 0 END
WHEN 1 THEN N'st'
WHEN 2 THEN N'nd'
WHEN 3 THEN N'rd'
ELSE N'th'
END
+ ' ' + DATENAME(dw, #dt)
+ ' of ' + DATENAME(M, #dt)
+ ', ' + CAST(YEAR(#dt) AS NVARCHAR)
Result is a single SELECT (provided the assignment of #dt happened earlier) but is, essentially, the same logic as yours.
This following code will give you DATE for any day of the week in any month or year that you specify. All the variables that I have are to reduce repeating logic to improve code speed.
This code gives you date for 1st Monday in February in 2013
DECLARE #DayNumber INT = 1
,#DayWeekNumber INT = 2
,#MonthNumber INT = 2
,#YearNumber INT = 2013
,#FoM DATE
,#FoMWD INT;
SET #FoM = DATEFROMPARTS(#YearNumber,#MonthNumber,1)
SET #fomwd = DATEPART(WEEKDAY, #FoM);
SELECT CASE WHEN #fomwd = #DayWeekNumber THEN DATEADD(WEEK, #DayNumber - 1, #FoM)
WHEN #fomwd < #DayWeekNumber THEN DATEADD(DAY, #DayWeekNumber - #fomwd, DATEADD(WEEK, #DayNumber - 1, #FoM))
WHEN #fomwd > #DayWeekNumber THEN DATEADD(DAY, #DayWeekNumber - #fomwd, DATEADD(WEEK, #DayNumber, #FoM))
END AS DateOfDay;

Using DatePart in line of a larger sql clause

This statement will correctly merge 2 columns ('DATE' and 'TIME'):
update AllBW1 set sUserTime =
CAST(
(
STR( YEAR( [DATE] ) ) + '/' +
STR( MONTH( [DATE] ) ) + '/' +
STR( DAY( [DATE] ) ) + ' ' +
(select DATENAME(hour, [TIME]))+ ':' +
(select DATENAME(minute, [TIME])) + ':' +
(select DATENAME(SECOND, [TIME]))
) as DATETIME)
where sUserTime is null
I'd like to refine the above so as to replace the default timezone with my own (GMT-6).
I've tried a few variations:
CAST((select DATEADD(hour, -6, DATENAME(hour, [TIME]))) as smalldatetime) + ':' +
and
(select CAST(DATEADD(hour, -6, DATENAME(hour, [TIME]))) as datetime) + ':' +
and have achieved no joy.
thx
The logfile as parsed into the SQL table by LogParser 2.2 has separate fields for Date and Time but, since both are formatted as datatime fields they end up looking like:
2012-01-04 00:00:00.000 for date (all time fields are zeroed)
2012-01-01 06:04:41.000 for time (all date field = first day of current year)
That's the reason the query is parsing each element the way it is. Thanks to Dems comment I simplified everything down. I've no doubt this can be optimized by for the volumes I'm dealing with this is adaquate:
update myTable set sUserTime =
(
DATENAME(YEAR, [DATE] ) + '/' +
DATENAME(MONTH, [DATE] ) + '/' +
DATENAME(DAY, [DATE] ) + ' ' +
DATENAME(hour, (dateadd(hh, -6, [time])))+ ':' +
DATENAME(minute, [TIME]) + ':' +
DATENAME(SECOND, [TIME])
)
where sUserTime is null

How to convert date into DD-MON-YYYY HH24:MI:SS format?

Can someone help me with SQL Date format?
The following statement
SELECT convert(VARCHAR(20),GETDATE(),113)
returns
04 Aug 2011 08:08:08.
I want the results like
Aug-04-2011 08:08:08
Thank you!
SELECT
LEFT(DATENAME(MONTH, Date), 3) + '-' +
RIGHT(100 + DAY(Date), 2) + '-' +
DATENAME(YEAR, Date) + ' ' +
CONVERT(varchar, Date, 108)
FROM (SELECT Date = GETDATE()) s
Off the top of my head, I think its:
SELECT convert(VARCHAR(20),GETDATE(),120)
EDIT:
This will work:
SELECT datename(day, GETDATE()) + '-'
+ substring(datename(month, GETDATE()),0,4) + '-'
+ datename(year, GETDATE()) + ' '
+ datename(hh, GETDATE()) + ':'
+ datename(mi, GETDATE()) + ':'
+ datename(ss, GETDATE())
SECOND EDIT:
SELECT substring(datename(month, GETDATE()),0,4) + '-'
+ datename(day, GETDATE()) + '-'
+ datename(year, GETDATE()) + ' '
+ datename(hh, GETDATE()) + ':'
+ datename(mi, GETDATE()) + ':'
+ datename(ss, GETDATE())
THIRD EDIT:
select substring(datename(month, GETDATE()),0,4) + '-'
+ right(datename(day, GETDATE())+100,2) + '-'
+ datename(year, GETDATE()) + ' '
+ right(datename(hh, GETDATE())+100,2) + ':'
+ right(datename(mi, GETDATE())+100,2) + ':'
+ right(datename(ss, GETDATE())+100,2)
The built-in convert won't allow you to format your date exactly as you desire, unfortunately.
With a little manipulation, you can get there though:
SELECT stuff(stuff(convert(VARCHAR(20),GETDATE(),113), 3, 1, '-'), 7, 1, '-')
You could put this in a UDF and call that whenever you want your date formatted in this manner.