October 5, 2018 at 2:32 am
Hi Sir,
How to Init-cap the characters in SQL server in SQL query without creating any function?
create table abc
(Month_Name nvarchar(10))
INSERT INTO abc VALUES ('APRIL');
INSERT INTO abc VALUES ('AUGUST');
INSERT INTO abc VALUES ('DECEMBER');
INSERT INTO abc VALUES ('FEBRUARY');
INSERT INTO abc VALUES ('JANUARY');
INSERT INTO abc VALUES ('JULY');
INSERT INTO abc VALUES ('JUNE');
INSERT INTO abc VALUES ('MARCH');
INSERT INTO abc VALUES ('MAY');
INSERT INTO abc VALUES ('NOVEMBER');
INSERT INTO abc VALUES ('OCTOBER');
INSERT INTO abc VALUES ('SEPTEMBER');
My out put should show like below
April
August
December
February
January
July
June
March
May
November
October
September
October 5, 2018 at 7:17 am
You could do this:
SELECT UPPER(LEFT(Month_Name,1)) + LOWER(SUBSTRING(Month_Name,2,8))
FROM abc
October 5, 2018 at 7:36 am
laurie-789651 - Friday, October 5, 2018 7:17 AMYou could do this:
SELECT UPPER(LEFT(Month_Name,1)) + LOWER(SUBSTRING(Month_Name,2,8))
FROM abc
This solves only the case for single word capitalization and only words up to 9 characters long, which is the case for month names, but perhaps little else. I thought I remembered seeing a post here sometime in the last 6 months that creates a function that delivers something like the functionality of the Proper() function from somewhere in my past... not sure if it was VBA or VB or Crystal Reports.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
October 5, 2018 at 7:40 am
sgmunson - Friday, October 5, 2018 7:36 AMlaurie-789651 - Friday, October 5, 2018 7:17 AMYou could do this:
SELECT UPPER(LEFT(Month_Name,1)) + LOWER(SUBSTRING(Month_Name,2,8))
FROM abcThis solves only the case for single word capitalization and only words up to 9 characters long, which is the case for month names, but perhaps little else. I thought I remembered seeing a post here sometime in the last 6 months that creates a function that delivers something like the functionality of the Proper() function from somewhere in my past... not sure if it was VBA or VB or Crystal Reports.
Also note that the OP doesn't want to create a function. Not sure why, but that was the requirement.
October 5, 2018 at 7:42 am
Lynn Pettis - Friday, October 5, 2018 7:40 AMsgmunson - Friday, October 5, 2018 7:36 AMlaurie-789651 - Friday, October 5, 2018 7:17 AMYou could do this:
SELECT UPPER(LEFT(Month_Name,1)) + LOWER(SUBSTRING(Month_Name,2,8))
FROM abcThis solves only the case for single word capitalization and only words up to 9 characters long, which is the case for month names, but perhaps little else. I thought I remembered seeing a post here sometime in the last 6 months that creates a function that delivers something like the functionality of the Proper() function from somewhere in my past... not sure if it was VBA or VB or Crystal Reports.
Also note that the OP doesn't want to create a function. Not sure why, but that was the requirement.
Yep... I know. Not usually practical without a function, at least for the general case. For this specific case, there's an easy way, but the number of such easy cases is probably pretty limited in scope.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
October 5, 2018 at 7:50 am
If your data is really just the twelve months of the year why not simply update the table? It would take about 2 minutes. But if that is just an example then use the string manipulation methods posted above.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 5, 2018 at 8:10 am
This should be a function but the either the OP or the OP's DBA Team thinks they should be avoided because of some "Best Practice" someone found somewhere stating that functions should be avoided. Creating an iSF using a scalar return from an iTVF would be the way to go here.
In case someone reading this has no clue as to what I'm talking about, especially when it comes to performance, please see the following. The proper use of functions will greatly simplify life through the use of encapsulation.
How to Make Scalar UDFs Run Faster (SQL Spackle)
--Jeff Moden
Change is inevitable... Change for the better is not.
October 5, 2018 at 2:33 pm
A utility table would be more efficient and if you are using a case insensitive collation all it would need is the actual list of proper cased months. If you must use a scalar function I would use CLR. The presentation layer is the best place to manipulate strings as you are typically presenting a small number of rows at a time so it's much faster.
October 5, 2018 at 6:57 pm
kiran.rajenimbalkar - Friday, October 5, 2018 2:32 AMHi Sir,How to Init-cap the characters in SQL server in SQL query without creating any function?
create table abc
(Month_Name nvarchar(10))
INSERT INTO abc VALUES ('APRIL');
INSERT INTO abc VALUES ('AUGUST');
INSERT INTO abc VALUES ('DECEMBER');
INSERT INTO abc VALUES ('FEBRUARY');
INSERT INTO abc VALUES ('JANUARY');
INSERT INTO abc VALUES ('JULY');
INSERT INTO abc VALUES ('JUNE');
INSERT INTO abc VALUES ('MARCH');
INSERT INTO abc VALUES ('MAY');
INSERT INTO abc VALUES ('NOVEMBER');
INSERT INTO abc VALUES ('OCTOBER');
INSERT INTO abc VALUES ('SEPTEMBER');My out put should show like below
April
August
December
February
January
July
June
March
May
November
October
September
Shifting gears a bit, what are you actually going to use this for? Depending on where the data is coming from, what the data is a part of (in this case, is it a part of an actual date?), and what it's use is after the transformation, there may be better alternatives that just doing the initial caps thing.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2018 at 6:46 pm
Jeff Moden - Friday, October 5, 2018 6:57 PMShifting gears a bit, what are you actually going to use this for?
Passing an interview?
_____________
Code for TallyGenerator
October 8, 2018 at 8:49 pm
Sergiy - Monday, October 8, 2018 6:46 PMJeff Moden - Friday, October 5, 2018 6:57 PMShifting gears a bit, what are you actually going to use this for?
Passing an interview?
Must be it. No one asked how to handle "MCDONALD" or "VAN DYCK". 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
October 9, 2018 at 1:55 am
Jeff Moden - Monday, October 8, 2018 8:49 PMSergiy - Monday, October 8, 2018 6:46 PMJeff Moden - Friday, October 5, 2018 6:57 PMShifting gears a bit, what are you actually going to use this for?
Passing an interview?
Must be it. No one asked how to handle "MCDONALD" or "VAN DYCK". 😀
Apostrophes 😛
Far away is close at hand in the images of elsewhere.
Anon.
October 9, 2018 at 7:24 am
David Burrows - Tuesday, October 9, 2018 1:55 AMJeff Moden - Monday, October 8, 2018 8:49 PMSergiy - Monday, October 8, 2018 6:46 PMJeff Moden - Friday, October 5, 2018 6:57 PMShifting gears a bit, what are you actually going to use this for?
Passing an interview?
Must be it. No one asked how to handle "MCDONALD" or "VAN DYCK". 😀
Apostrophes 😛
And dashes and titles, too! 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
October 10, 2018 at 1:29 pm
USE Jic
GO
CREATE TABLE dbo.abc (Month_Name nvarchar(10) NOT NULL PRIMARY KEY)
GO
INSERT dbo.abc ( Month_Name )
VALUES (N'APRIL')
, (N'AUGUST')
, (N'DECEMBER')
, (N'FEBRUARY')
, (N'JANUARY')
, (N'JULY')
, (N'JUNE')
, (N'MARCH')
, (N'MAY')
, (N'NOVEMBER')
, (N'OCTOBER')
, (N'SEPTEMBER') ;
CREATE TABLE dbo.ProperMonths (ProperMonthName nvarchar(9) NOT NULL PRIMARY KEY);
GO
INSERT dbo.ProperMonths ( ProperMonthName )
VALUES ( N'April')
, ( N'August')
, ( N'December')
, ( N'February')
, ( N'January')
, ( N'July')
, ( N'June')
, ( N'March')
, ( N'May')
, ( N'November')
, ( N'October')
, ( N'September') ;
SELECT a.Month_NameOctober 10, 2018 at 2:01 pm
Joe Torre - Wednesday, October 10, 2018 1:29 PMUSE Jic
SELECT a.Month_Name
GO
CREATE TABLE dbo.abc (Month_Name nvarchar(10) NOT NULL PRIMARY KEY)
GO
INSERT dbo.abc ( Month_Name )
VALUES (N'APRIL')
, (N'AUGUST')
, (N'DECEMBER')
, (N'FEBRUARY')
, (N'JANUARY')
, (N'JULY')
, (N'JUNE')
, (N'MARCH')
, (N'MAY')
, (N'NOVEMBER')
, (N'OCTOBER')
, (N'SEPTEMBER') ;
CREATE TABLE dbo.ProperMonths (ProperMonthName nvarchar(9) NOT NULL PRIMARY KEY);
GO
INSERT dbo.ProperMonths ( ProperMonthName )
VALUES ( N'April')
, ( N'August')
, ( N'December')
, ( N'February')
, ( N'January')
, ( N'July')
, ( N'June')
, ( N'March')
, ( N'May')
, ( N'November')
, ( N'October')
, ( N'September') ;
, p.ProperMonthName
FROM dbo.abc a
JOIN dbo.ProperMonths p ON a.Month_Name = p.ProperMonthName;
DROP TABLE dbo.abc;
DROP TABLE dbo.ProperMonths;
GO
And if your server or database is using a case sensitive collation?
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply