How to show PRINT results in EXCEL - sql

A team member and I re-wrote a query we posted here due to issues; using various sources from provided answers from my previous question as well as checking other solutions and the query works now as intended. HOWEVER we need to embed this query and run in the background (we already have) into an EXCEL sheet but the issue is we need to somehow actually display the results in the cell. The PRINT function of the query actually pops up a window in EXCEL when we run (DUH, as intended) but what we would like to do is some how show the PRINTED results. Since the query is run automagically inside the spreadsheet the option to export from SSMS to a file then retrieve from there is not an option since OUR DBA is pretty funky on what we do with the DB we cannot run as a job..etc.... Any help again would be appreciated.
Declare #StartDate1 as DateTime
Declare #StartDate2 as DateTime
Declare #EndDate1 as DateTime
Declare #EndDate2 as DateTime
Declare #TodaysDate1 as DateTime
Declare #TodaysDate2 as DateTime
Declare #Yesterday as DateTime
Declare #TotalRecords1 int = 7
Declare #TotalRecords2 int = 7
set #TodaysDate1 = GETDATE()
set #TodaysDate2 = #TodaysDate1
set #Yesterday = DATEADD(day,-1,#TodaysDate1)
-------------------------------------------------------------------
-- For AM Shift Data Readings --
-------------------------------------------------------------------
set #StartDate1 = cast(convert(varchar(4), datepart(yyyy, #Yesterday)) + '-' +
convert(varchar(2), datepart(mm, #Yesterday)) + '-' +
convert(varchar(2), datepart(dd, #Yesterday)) + ' ' +
'17:00' as datetime)
set #EndDate1 = cast(convert(varchar(4), datepart(yyyy, #TodaysDate1)) + '-' +
convert(varchar(2), datepart(mm, getdate())) + '-' +
convert(varchar(2), datepart(dd, getdate())) + ' ' +
'04:59' as datetime)
-------------------------------------------------------------------
-- For PM Shift Data Readings --
-------------------------------------------------------------------
set #StartDate2 = cast(convert(varchar(4), datepart(yyyy, #Yesterday)) + '-' +
convert(varchar(2), datepart(mm, #Yesterday)) + '-' +
convert(varchar(2), datepart(dd, #Yesterday)) + ' ' +
'05:00' as datetime)
set #EndDate2 = cast(convert(varchar(4), datepart(yyyy, #Yesterday)) + '-' +
convert(varchar(2), datepart(mm, #Yesterday)) + '-' +
convert(varchar(2), datepart(dd, #Yesterday)) + ' ' +
'16:59' as datetime)
--------Material Scan Table--------
SELECT
#TotalRecords1=(SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate1 and #EndDate1),
#TotalRecords2=(SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate2 and #EndDate2)
WHILE #TotalRecords1 = 0 AND #TotalRecords2 = 0
BEGIN
set #TodaysDate1 = DATEADD(day,-1,#TodaysDate1)
set #TodaysDate2 = DATEADD(day,-1,#TodaysDate2)
set #Yesterday = DATEADD(day,-1,#TodaysDate1)
-------------------------------------------------------------------
-- For AM Shift Data Readings --
-------------------------------------------------------------------
set #StartDate1 = cast(convert(varchar(4), datepart(yyyy, #Yesterday)) + '-' +
convert(varchar(2), datepart(mm, #Yesterday)) + '-' +
convert(varchar(2), datepart(dd, #Yesterday)) + ' ' +
'17:00' as datetime)
set #EndDate1 = cast(convert(varchar(4), datepart(yyyy, #TodaysDate1)) + '-' +
convert(varchar(2), datepart(mm, getdate())) + '-' +
convert(varchar(2), datepart(dd, getdate())) + ' ' +
'04:59' as datetime)
-------------------------------------------------------------------
-- For PM Shift Data Readings --
-------------------------------------------------------------------
set #StartDate2 = cast(convert(varchar(4), datepart(yyyy, #TodaysDate2)) + '-' +
convert(varchar(2), datepart(mm, getdate())) + '-' +
convert(varchar(2), datepart(dd, getdate())) + ' ' +
'05:00' as datetime)
set #EndDate2 = cast(convert(varchar(4), datepart(yyyy, #TodaysDate2)) + '-' +
convert(varchar(2), datepart(mm, getdate())) + '-' +
convert(varchar(2), datepart(dd, getdate())) + ' ' +
'16:59' as datetime)
SELECT
#TotalRecords1=(SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate1 and #EndDate1),
#TotalRecords2=(SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate2 and #EndDate2)
IF (SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate1 and #EndDate1) > 0
BREAK
ELSE
IF (SELECT Count(*) FROM [Piston_TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component] WHERE Close_Time_Stamp between #StartDate2 and #EndDate2) > 0
BREAK
ELSE
CONTINUE
END
PRINT '1st Shift LOT Trace Scans ending'
PRINT #EndDate2
PRINT #TotalRecords2
PRINT ' '
PRINT '2nd Shift LOT Trace Scans ending'
PRINT #EndDate1
PRINT #TotalRecords1
THESE ARE THE ACTUAL RESULTS from run query in SSMS we would like to somehow get EXCEL to show:
1st Shift LOT Trace Scans ending
Jul 6 2016 4:59PM
64
2nd Shift LOT Trace Scans ending
Jul 7 2016 4:59AM
73

You could try saving these into a table variable instead of printing them as follows:
declare #msgTable table ( msg varchar(100));
insert into #msgTable values ('1st Shift LOT Trace Scans ending');
insert into #msgTable values (#EndDate2);
select * from #msgTable;
and then right clicking the results and clicking Save results as to a csv file or a tab delimited file both of which you can open in excel.

So after a long weekend and help from the team we were able to get the results in EXCEL we want and pretty simple really. All we needed to do instead of PRINT was use CONCAT. I thought since we did eventually arrive at a conclusion I would post here. Though the answer above is also a somewhat solution the one I am posting here works more to our liking to avoid have to export the data when we can import automatically via the query. Thanks to all who chimed in!See bellow:
SELECT CONCAT ( '1st Shift ', #StartDate1, ' Total Scans ', #TotalRecords1, ' and ' +
'2nd Shift ', #StartDate2, ' Total Scans ', #TotalRecords2 ) as LotTrace
And the result was:
LotTrace
1st Shift Jul 11 2016 5:00PM Total Scans 105 2nd Shift Jul 11 2016 5:00AM Total Scans 77

Related

SQL - Run second Query if count result = 0

Both my SELECT queries work below and return values with as two separate results. What I would like to do is if the first SELECT statement returns a 0 and it will sometimes due to the nature of our work, I would like the second to run ignoring the first one that returns zero if possible. IF the first one returns a result greater than zero then I do not want the second to run.
I have been all over looking and tried different IF ELSE and such but because of the specific date/time formulas used I am thinking it may not be possible or I this may have to be re-written altogether. There is a purpose for this but I will not bore you. Thanks in advance!
Declare #StartDate as DateTime
Declare #EndDate as DateTime
Declare #TodaysDate as DateTime
Declare #Previous as DateTime
Declare #Previous2 as DateTime
set #TodaysDate = GETDATE()
set #Previous = DATEADD(day,-1,#TodaysDate)
set #Previous2 = DATEADD(day,-2,#TodaysDate)
-- SELECT Statetment one starts here
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous)) + '-' +
convert(varchar(2), datepart(dd, #Previous)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous)) + '-' +
convert(varchar(2), datepart(dd, #Previous)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
-- Query 2 Starts, Declarations already made at beginning
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
You can try to store the value of your first query result in a variable and then then check the value like
declare #myVar int
SELECT #myVar = Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate)
if(#myVar = 0)
//Here your second query logic
begin
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
end
use NOT Exists..
if NOT Exists
(Select 1 as FirstShfitPrevious--first query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
)
Begin
SELECT Count(*) as FirstShfitPrevious2--second query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
END
Else
Begin
--First query
End
or even that way:
SELECT TOP 1 Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
GROUP BY CAST(Close_Time_Stamp AS DATE)
ORDER BY CAST(Close_Time_Stamp AS DATE) DESC
with period covering both date ranges.

SQL - Email query range

I have a sql job which queries the database. The job is scheduled to run every 24 hours and it sends out an email with required data which has a query range from 07:30 today to 07:30 the previous day. Here is the code for the heading of my email :
INSERT INTO #ReportContentBuilder VALUES('<h4>Query Range : ' + DATENAME(WEEKDAY,#StartTimestamp)
+ ', ' + CONVERT(varchar, #StartTimestamp, 106) + ' ' + CONVERT(varchar, #StartTimestamp, 108)
+ ' (UTC) to ' + DATENAME(WEEKDAY,#EndTimestamp) + ', ' + CONVERT(varchar, #EndTimestamp, 106)
+ ' ' + CONVERT(varchar, #EndTimestamp, 108) + ' (UTC)</h4>')
Here is the value I have for #StartTimestamp:
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, #EndTimestamp), 106) + ' 07:30') as datetime)
Here is the expected output for my email heading :
Query Range : Wednesday, 19 Nov 2014 07:30:00 (UTC) to Thursday, 20 Nov 2014 07:30:00 (UTC)
My question is what value do I use for #EndTimestamp??
If I use GETUTCDATE() and the job runs 2 mins late then the range is incorrect. I also don't want to hardcode it because of the changes needed for daylight savings each time.
The trick is that the "dynamic" part is the difference between current time (GETUTCDATE()) and 7:30 . So with #timeDiff you can handle this dynamic part of the ecuation. Try this (replace last select with your needed insert; I only tested the output):
DECLARE #StartTimestamp datetime
DECLARE #EndTimestamp datetime = GETUTCDATE()
DECLARE #timeDiff time
SET #timeDiff = CONVERT(time, (#EndTimestamp - cast('1900-01-01 07:30:00.000' as datetime)))
SELECT #EndTimestamp = dateadd(second,
datepart(hour,#timeDiff) * -3600 +
datepart(minute,#timeDiff) * -60 +
datepart(second,#timeDiff) * -1,
#EndTimestamp)
SET #StartTimestamp = DATEADD(DAY, -1, #EndTimestamp)
SELECT #StartTimestamp, #EndTimestamp
SELECT '<h4>Query Range : ' + DATENAME(WEEKDAY,#StartTimestamp)
+ ', ' + CONVERT(varchar, #StartTimestamp, 106) + ' ' + CONVERT(varchar, #StartTimestamp, 108)
+ ' (UTC) to ' + DATENAME(WEEKDAY,#EndTimestamp) + ', ' + CONVERT(varchar, #EndTimestamp, 106)
+ ' ' + CONVERT(varchar, #EndTimestamp, 108) + ' (UTC)</h4>'

Concatenating two TIME columns in SQL Server 2012

I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)

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

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'.

How do I get the month and day with leading 0's in SQL? (e.g. 9 => 09)

DECLARE #day CHAR(2)
SET #day = DATEPART(DAY, GETDATE())
PRINT #day
If today was the 9th of December, the above would print "9".
I want to print "09". How do I go about doing this?
Pad it with 00 and take the right 2:
DECLARE #day CHAR(2)
SET #day = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, GETDATE())), 2)
print #day
For SQL Server 2012 and up , with leading zeroes:
SELECT FORMAT(GETDATE(),'MM')
without:
SELECT MONTH(GETDATE())
Use SQL Server's date styles to pre-format your date values.
SELECT
CONVERT(varchar(2), GETDATE(), 101) AS monthLeadingZero -- Date Style 101 = mm/dd/yyyy
,CONVERT(varchar(2), GETDATE(), 103) AS dayLeadingZero -- Date Style 103 = dd/mm/yyyy
Try this :
SELECT CONVERT(varchar(2), GETDATE(), 101)
Leading 0 day
SELECT FORMAT(GetDate(), 'dd')
SQL Server 2012+ (for both month and day):
SELECT FORMAT(GetDate(),'MMdd')
If you decide you want the year too, use:
SELECT FORMAT(GetDate(),'yyyyMMdd')
Select Replicate('0',2 - DataLength(Convert(VarChar(2),DatePart(DAY, GetDate()))) + Convert(VarChar(2),DatePart(DAY, GetDate())
Far neater, he says after removing tongue from cheek.
Usually when you have to start doing this sort of thing in SQL, you need switch from can I, to should I.
SELECT RIGHT('0'
+ CONVERT(VARCHAR(2), Month( column_name )), 2)
FROM table
Might I suggest this user defined function if this what you are going for:
CREATE FUNCTION dbo.date_code (#my_date date) RETURNS INT
BEGIN;
DECLARE #retval int;
SELECT #retval = CAST(CAST(datepart(year,#my_date) AS nvarchar(4))
+ CONVERT(CHAR(2),#my_date, 101)
+ CONVERT(CHAR(2),#my_date, 103) AS int);
RETURN #retval;
END
go
To call it:
SELECT dbo.date_code(getdate())
It returns as of today
20211129
Roll your own method
This is a generic approach for left padding anything. The concept is to use REPLICATE to create a version which is nothing but the padded value. Then concatenate it with the actual value, using a isnull/coalesce call if the data is NULLable. You now have a string that is double the target size to exactly the target length or somewhere in between. Now simply sheer off the N right-most characters and you have a left padded string.
SELECT RIGHT(REPLICATE('0', 2) + CAST(DATEPART(DAY, '2012-12-09') AS varchar(2)), 2) AS leftpadded_day
Go native
The CONVERT function offers various methods for obtaining pre-formatted dates. Format 103 specifies dd which means leading zero preserved so all that one needs to do is slice out the first 2 characters.
SELECT CONVERT(char(2), CAST('2012-12-09' AS datetime), 103) AS convert_day
DECLARE #day CHAR(2)
SET #day = right('0'+ cast(day(getdate())as nvarchar(2)),2)
print #day
use
CONVERT(CHAR(2), DATE_COLUMN, 101)
to get the month part with 2 characters and
CONVERT(CHAR(2), DATE_COLUMN, 103)
for the day part.
Declare #dateToGet varchar(10)
Set #dateToGet = convert(varchar, getdate(), 112)
This works fine for the whole date with leading zeros in month and day
select
right('0000' + cast(datepart(year, GETDATE()) as varchar(4)), 4) + '-'+ +
right('00' + cast(datepart(month, GETDATE()) as varchar(2)), 2) + '-'+ +
right('00' + cast(datepart(day, getdate()) as varchar(2)), 2) as YearMonthDay