convert to_char(..,'MON-YYYY') into sql server - sql-server

I have the following query with plsql
SELECT to_char(add_months(l_max_date, l_rec.MON_INTERVAL),'MON-YYYY')
FROM dual
I am looking to write it with sql sever
select CONVERT(VARCHAR(2),MONTH(DATEADD(mm, MON_INTERVAL, l_max_date)))
from rec
How to write
to_char(..,'MON-YYYY') into sql server

There is no default date format in SQL Server like that. You have to come up with your own.
SELECT datename(month, getdate())
+ '-'
+ convert(nvarchar, year(getdate()))

In SQL Server 2012+ you can use the FORMAT function to format a value using the same formatting string you would use in .NET:
SELECT FORMAT(GETDATE(),'MMM-yyyy')

You could do this:
SELECT
SubString(Convert(Varchar(Max), tbl.theDate,0), 1, 3) + '-'
+ Cast(Year(tbl.theDate) As Varchar(Max))
FROM
(
select CONVERT(VARCHAR(2),MONTH(DATEADD(mm, MON_INTERVAL, l_max_date))) as theDate
from rec
) AS tbl

Related

How to get 3 letter abbreviation for month in SQL

How to get month in 3 letters in SQL.
In SQL Table data is inserted:
2016-01-07 09:38:58.310
I need only month result in 3 letters like below:
Jan
Assuming you're using SQL Server 2012 or newer, you can use the FORMAT function:
SELECT FORMAT([Date], 'MMM', 'en-US')
Adapt the locale as needed.
Since you're on SQL Server 2008, I'd use
SELECT LEFT(DATENAME(MONTH, [Date]), 3)
Try this (I am assuming you are using Sql Server).
Select Convert(char(3), GetDate(), 0)
If you need full name of month, try
Select Datename(month, GetDate())
Or you could just do:
LEFT(GETDATE(), 3)
For instance you could declare a variable:
Declare #MONTH VARCHAR(3)
Set #MONTH = LEFT(GETDATE(), 3)

SQL Server convert date to String MMMYYDD

Im trying to convert date value into this MMMYYDD String format, using native Sql server 2008 r2 function,
I checked here http://www.sql-server-helper.com/tips/date-formats.aspx
and still I can't find matching format.
any ideas?
something like this?
select convert(varchar(3), #date, 7) + convert(varchar(4), #date, 12)
sql fiddle demo

How to produce JSON strings from SQL Server queries via TSQL?

I wanted to know if there is any function or something to convert the SQL select query result to JSON string format?
For example, SQL select query result is,
current target
-----------------
500 1000
1500 2000
JSON result:
[{"current":500,"target":1000},{"current":1500,"target":2000}]
Any ideas will be helpful.
Thanks.
SQL Fiddle
MS SQL Server 2008 Schema Setup:
Query 1:
DECLARE #TABLE TABLE ([current] INT, [target] INT)
INSERT INTO #TABLE VALUES
(500 , 1000),
(1500 , 2000)
SELECT '[' + STUFF((SELECT ',{"current":' + CAST([current] AS VARCHAR(30))
+ ',"target":' + CAST([target] AS VARCHAR(30)) + '}'
FROM #TABLE
FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,1,'') + ']'
Results:
[{"current":500,"target":1000},{"current":1500,"target":2000}]
You don't specify version.
In SQL Server 2016 you will be able to do something like
SELECT [current],
target
FROM YourTable
ORDER BY [current]
FOR JSON AUTO;
More details here or in the official pre release documentation
I use
SELECT
JSON_QUERY(( SELECT
[current],target
FROM YourTable
FOR JSON PATH
))
Works well with minimal effort. I generally convert the output to a List<Dictionary<string,dynamic>> in C#/.Net (if I don't have an existing model).

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

sql select datetime stamp as m/d/y only. (without h m s)

When I select two rows with a DATETEIME stamp, I only want the m/d/y data and nothing after that.
It has to changed during the select (not afterwards).
To remove the time you just need to do the following Assuming SQL Server
Pre SQL 2008
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) yourdate
FROM yourtable
SQL 2008
select CAST(getdate() as date) yourdate
FROM yourtable
See Most efficient way in SQL Server to get date from date+time?
or
Best approach to remove time part of datetime in SQL Server
Is this for export? If you only want the text you can use a variety of coversion formats available on MSDN.
select convert(varchar, getdate(), 101)
-- output: 07/05/2011
Otherwise, if you're using sql 2008, you can just cast the datetime to date:
select cast(getdate() as date)

Resources