Extract only month from SQL Query - sql-server

I've a SQl Query. I'm using dropdownlist to display the dates. I'd like to display the month in MMMM format.
SELECT DISTINCT[drdates] (CONVERT(CHAR(4), [drdates], 100) + CONVERT(CHAR(4), [drdates], 120)) FROM [DRReceive_20141229]

SELECT DISTINCT UPPER(LEFT(DATENAME(MONTH,MONTH([drdates])),4))
+ CONVERT(CHAR(4), [drdates], 120)
FROM [DRReceive_20141229]
On a side note I have never seen date values being formatted as MMMMyyyy, a rather strange format to show date values.
But if you wanted something rather simple or usual format like MMMyyyy and if you are using sql server 2012 or later version you can do the following ...
SELECT DISTINCT UPPER(FORMAT ( [drdates], 'MMMyyyy' ))
FROM [DRReceive_20141229]

This will help you to retrieve months first four characters :
select CONVERT(CHAR(4),DATENAME(MONTH, [drdates])) + CONVERT(CHAR(4),[drdates], 120)) [DRReceive_20141229];

Related

To check the old dates present or not

I have list of dates like 19/2/2017, 20/8/1975, 02/03/1989, 04/08/2015 . I need a query by using SQL server to find year which is less than current year.
eg: 19/2/2017, 20/8/1975, 02/03/1989
by using the query, it should display these two 20/8/1975, 02/03/1989
Hope this helps
SELECT * FROM YourTable WHERE YEAR(DateFiled) < YEAR(GETDATE())
To guarantee you convert the dates correctly, independently of the SQL Server ##DATEFORMAT
DECLARE #BadDateStorage table (BadData varchar(12));
INSERT #BadDateStorage (BadData)
VALUES ('02/03/1989'), ('20/8/1975'), ('019/2/2017');
SELECT CONVERT(smalldatetime, BadData, 103)
FROM #BadDateStorage
WHERE CONVERT(smalldatetime, BadData, 103) < '20170101'

Convert DDMMYYYY to YYYYMMDD date format in sql server?

I have a date in table as "26052016" in format DDMMYYYY
I want to convert this date to "YYYYMMDD" format.
Any idea
I have tried this method
select CONVERT(varchar(8),[doc-date],112) FROM C034_PDK_ParallelBillingSourceExtract
But this is gives me the same date as a result.
Please help me
I can find this way, i don't know if any other way exist or not..
declare #date nvarchar(max)='01052016'
select convert(varchar(8),cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date),112)as [YYYYMMDD]
Clear Code:
declare #date nvarchar(max)='01052016'
declare #date1 date
set #date1 =cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date)
select convert(varchar(8),#date1,112)as [YYYYMMDD]
If you are using Sql version< 2012 then you need to skip CONCAT and use + for string concatination.
SELECT CONVERT(VARCHAR(8), doc-date, 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Check ... this should work correctly.
Thanks
SELECT CONVERT(VARCHAR(8), '26052016', 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Try like this,
SELECT substring([doc-date], 5, 4) + substring([doc-date], 3, 2) + substring([doc-date], 1, 2) AS [YYYYMMDD]
FROM C034_PDK_ParallelBillingSourceExtract
There are differet ways to do it.
The best way is to use substring method as you know the character positions are going to remain same.
For Example
Suppose your date is - 31122015
Pick the portions of date using substring method and concatenate them
select SUBSTRING('31122015',5,4) + SUBSTRING('31122015',3,2) + SUBSTRING('31122015',1,2)
The result would be - 20153112
Since SQL Server 2016 we have a couple of handy tools for this:
Use DATEFROMPARTS and SUBSTRING to convert odd date formats from any arrangement within a Varchar to an actual date:
SELECT DATEFROMPARTS(SUBSTRING('31122015',5,4), SUBSTRING('31122015',3,2), SUBSTRING('31122015',1,2))
Use FORMAT to Convert an actual date to YYYYMMDD:
SELECT FORMAT(MyDate, 'yyyyMMdd')
watch out for the yyyyMMdd, that's the only part of MS SQL that is case-sensitive. Lower case mm is "minutes" and upper case MM is "Month", upper case YYYY or DD is nothing, and will just add letters to your output!

Insert System date in dd/mm/yyyy hh:mi:ss format

Im trying to insert Checkout Date in dd/mm/yyyy hh:mi:ss format using the following query:
insert into Rawtransactions
(Card Number,Processing Date,CurrencyCode,Checkout Date
)
Values
(
#NewCardNumber,getdate(),'USD',CONVERT(VARCHAR(24),GETDATE(),113)
)
But It inserted date in this format 02 Sep 2015 14:45:09:390 instead of 02/09/2015 14:45:09:390. What is the right syntax for this?
EDITED - Please note that the Checkout Date is an nvarchar field and the Schema cant be changed now. I want to know how can I insert date in this field?
Try like this:
select CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + convert(VARCHAR(8), GETDATE(), 14)
However it is not recommended to store dates as varchar() as it may lead you to some formatting issues like this in future.
Dates are dates. You can't control the format in which the database stores them; they're just values with a date data type. If you want to use them later in an application and display them a certain way, then that is up to your application.

Using LIKE in ms sql server management studio

Question: When I use the LIKE in my query it is gray and my query does not return anything when I know if should.
I am not sure if there is a configuration setting I am missing but normally when I use MS sql server management studio terms like: UPDATE WHERE SELECT ADD all show as blue in my query window.
Example query:
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE [MainSiteDB].[dbo].[usr_user].[usr_lastLogin] LIKE '2014-11-10%'
In my table in that field there are many entries like: 2014-11-10 13:19:46.967
Like is not applicable for datetime or date field for SQL server. 'Like' is for varchar/char/text and other text related field
You can use between or <> sign
Select * from tblTable where Date between '2014 Jan 01' and '2014 Jan 31'
OR
Select * from tblTable where Date >= '2014 Jan 01' and Date <= '2014 Jan 31'
You can find in menu in Management studio menu under Tools->Option
then Fonts and color. Then you can change the customize color for
SQL keywords.
you Can Use it :
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, [MainSiteDB].[dbo].[usr_user].[usr_lastLogin]))= '2014-05-9'
You can cast the date part and then use the LIKE operator. Check the below script:
Select * from TableName Where colName like Cast('2014-09-25' as datetime)
select * from tablename
where CONVERT(varchar(10), getdate(), 105) = #inputdate
Below convert will fetch the datepart alone from your columnname, and then based on your input it will fetch all matching records.
SELECT CONVERT(varchar(10), getdate(), 105) --> 11-11-2014
Try this query. Using LIKE directly in DateTime filed is won't give expected result. Convert the DateTime field to specific format to Varchar and use LIKE.
101 - Format the DateTime to mm/dd/yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 101)
LIKE '05/22/2014%'
105 - Format the DateTime to mm-dd-yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 105)
LIKE '05-22-2014%'

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Resources