I have a date that I want to display in SQL Server like this:
Date=`2015-12-08`
O/p=Dec 15(I need output like this)
How can I do it? I tried like
SELECT
LEFT(DATENAME(MONTH, GETDATE()), 3) + ' ' + DATENAME(YEAR, GETDATE()) AS [Mon YYYY]`
But it returns output like Dec 2015 but I need it to be Dec 15
try
SELECT CONVERT(CHAR(4), GETDATE(), 100) + CONVERT(CHAR(4), GETDATE(), 120)
update :
Your can try if you want only jan 15
SELECT RIGHT(CONVERT(varchar, "yourdate",6),6)
try this
SELECT LEFT(CAST(Date as varchar(20)), 3) + '-' + RIGHT(CAST(YEAR(Date) as CHAR(4)), 2) as MonYr
FROM YourTable
This is what you are looking for
SELECT RIGHT(CONVERT(varchar, GetDate(),6),6)
Related
I am using SQL Server 2008 R2.
I have one field storing time as integer value.
I want to convert integer to time as follows.
IntegerValue TimeValue
-------------------------
1 00:01 AM
11 00:11 AM
123 01:23 AM
541 05:41 AM
1317 13:17 PM
Please reply.
Thanks.
SELECT
CONVERT(VARCHAR, coulmn / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, column% 100),2)
FROM YourTable
Try like this,
It is almost similar to #Mukund answer
SELECT c AS IntegerValue
,CONVERT(VARCHAR, c / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, c % 100), 2) AS TimeValue
FROM (
VALUES (1)
,(11)
,(123)
,(541)
,(1317)
) t(c)
I have a date range:
SET #declared_start_datetime = '11/02/2015 07:00:00'
SET #declared_end_datetime = '11/09/2015 18:00:00'
This was working until I realized it was pulling all records from 7:00 am on 2nd of November until 6:00 pm on November 9th.
What I need it to do is pull all records from those dates but exclude Saturday/Sunday and only between the hours of 7:00 and 18:00 for Monday-Friday.
I am using SQL Server going through a linked MYSQL server so I am using OPENQUERY. Here is an example of what I have so far.
DEClare #session varchar(max) = 'SELECT * FROM database.session WHERE CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),#converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),#converted_end_datetime, 120) + ''''''
SET #session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + #session + ''')'
exec(#session)
How do I exclude Saturday's and Sundays and adjust it to only pull the time where the business is open?
Edit:
DEClare #session varchar(max) = 'SELECT * FROM database.session WHERE ((CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),#converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),#converted_end_datetime, 120) + ''''')
AND (TIME(CREATIONTIMESTAMP) between ''''' + convert(nvarchar(50),#business_opens_time, 108) + ''''' and ''''' + convert(nvarchar(50),#business_closes_time, 108) + '''''))'
SET #session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + #session + ''')'
exec(#session)
select * from ##session
drop table ##session
This doesn't exclude Saturdays or Sundays yet but it is currently returning 0 results. if I change the AND to OR it will return results but not what I am looking for.
I am looking for Monday - Friday from 7:00 am until 6:00 pm for each of the days.
Ok,
This is a little tricky, I am trying to replace the dates in a SQL Query results with a standard date, based on the month.
For example:
Any dates that are in July get 20140701
August gets 20140801
I could use a case statement:
Case
When Datepart(mm, TxnDate) = 1 and Datepart(yy, TxnDate) = 2014 then TxnDate = 20140101
etc...
but that could get very long as the database goes back 5 years and the result sets cover different periods then.
Any quick suggestions would be greatly appreciated.
Thanks,
declare #mydate datetime
select #mydate = GETDATE()
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
select #mydate = GETDATE() - 10
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
should print 20140701 and 20140601
If you simply want to set every TxnDate to the first of its month, you can do this:
TxnDate = DATEADD(month, DATEDIFF(month, 0, TxnDate), 0)
If your requirement is more complicated than that, you will need to explain.
Try Something like this
SELECT REPLACE (CONVERT(VARCHAR(8), TxnDate, 112),SUBSTRING(CONVERT(VARCHAR(8), TxnDate, 112),7,8),'01') AS [YYYYMMDD]
hope this is what you are looking for
I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.
Declare #dt nvarchar(20)
Select #dt = Convert(nvarchar(20), SalesDate, 113) FROM SalesTable
The format I need is 'dd-MMM-yyyy' (eg. '05-Jul-2013') but I could not find the proper style number (eg. 113) for this particular format. Can any one help me with that please?
Try this:
Declare #dt NVARCHAR(20)
Select
#dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine
It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:
SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]
See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
I Think this is the best way to do it.
REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
Because you do not have to use varchar(11) or varchar(10) that can make problem in future.
SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
OR
SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
will return the particular date in the format of 'dd-mm-yyyy'
The result would be like this..
04-07-2016
select convert(varchar(11), transfer_date, 106)
got me my desired result of date formatted as 07 Mar 2018
My column transfer_date is a datetime type column and I am using SQL Server 2017 on azure
select FORMAT(getdate(), 'dd-MMM-yyyy') as DateToday
Other answers here seem to return columns that are quite wide.
This answer returns a varchar(11) which is all that is required for a date in dd-Mon-yyyy format.
'SalesDate' = CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),SalesDate, 106), ' ', '-')),
Try also:
select CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),getdate(), 106), ' ', '-'))
As of today, this gives a result as per: 22-Jul-2021
Using SQL Server 2008, I have built an SSIS variable with dynamic value base on current date. I would like to have its value to be Friday if the current day is Monday, and here is the expression built:
DATEPART("dw",GETDATE()) != 2?
RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -1, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE())) : RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -3, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE()))
Issue: it is not accurate when the month or year changed. Is there any better way to do this? Help would be appreciated. Thanks.
Will this work (you can substitute GETDATE() for #date, I just used that to easily test out different dates)
DECLARE #date DATETIME
SET #date = '2013-01-14'
SELECT
PrevFriday = CASE WHEN DATEPART(weekday, #date) <> 2 THEN #date
ELSE DATEADD(DAY, -3, #date)
END
UPDATE: Here is same, but done in SSIS Variable Expression:
DATEPART("dw", GETDATE()) != 2?
GETDATE():
DATEADD("dw", -3, GETDATE())
UPDATE #2: Here is how to return the previous Friday for ANY date, not just Monday
SELECT DATEADD(DAY, -1 - (DATEPART(weekday, #date) % 7), #date)