Get the 2 digit year in T-SQL
You can do ( YEAR( GETDATE() ) % 100 ) + 1
See GETDATE & YEAR
How to get year in format YY in SQL Server
Try
SELECT RIGHT(YEAR(Creation_Date), 2) YY
FROM Asset_Creation
WHERE ...
Sample output:
| YY |
------
| 10 |
| 11 |
| 13 |
Here is SQLFiddle demo
Is there a simpler method for getting the two digit year?
Honestly, just don't work with 2 digit years any more; learn the lessons of last century's mistake and use 4 digits.
If you "have" to, then you could use CONVERT
with a style code, and then just replace the characters with an empty string:
DECLARE @TestVariable varchar(100) = '1234',
@Date datetime = GETDATE();
SELECT @TestVariable + REPLACE(CONVERT(varchar(8),@Date, 1),'/','');
T-SQL how to get four digit year value using two digit year part
So based on @Gordon Linoff answer function will look as below:
create function dbo.your_name
(
@yearYY nvarchar(2)
)
RETURNS nvarchar(4)
as
begin
declare @yearYYYY nvarchar(4)
set @yearYYYY = (SELECT ((CASE WHEN @yearYY >= 50 THEN '19'
ELSE '20' END) + @yearYY
) as yyyy)
return @yearYYYY
end
test:
select dbo.your_name ('70')
How can i select a datetime as ddMMyy, so a two-digit year?
As the docs for CONVERT
state, use 103
if you want the century and 3
if you dont:
REPLACE(CONVERT(CHAR(10), ChargeArrival.CreatedAt, 3), '/', '')
Two Digit date format in SQL
For the date part, to get the format you want you, try this:
SELECT
RIGHT(REPLICATE('0', 2) + CAST(DATEPART(DD, accid) AS VARCHAR(2)), 2) +
RIGHT(REPLICATE('0', 2) + CAST(DATEPART(MM, accid) AS VARCHAR(2)), 2) +
RIGHT(DATEPART(YY, accid), 2) AS CustomFormat
FROM yourtablename
...
The DATEPART(DD, accid)
will give you the day part and the same for mm
and yy
will give you the month and the year parts. Then I added the functions RIGHT(REPLICATE('0', 2) + CAST(... AS VARCHAR(2)), 2)
to add the leading zero, instead of 1
it will be 01
.
- SQL Fiddle Demo
As @bernd-linde suggested, you can use this function to concatenate it with the name part like:
concat(Name, ....) AS ...
Also you can just SELECT
or UPDATE
depending on what you are looking for.
As in @bernd-linde's fiddle.
what to do get a two digit number by using datepart () in sql?
Try this for MySql or from SQL Server 2012 -
select FORMAT(DATEPART(hour,'1900-01-01 07:45:00.010'),'00')
For SQL Server before 2012 -
select right('0' + DATEPART(hour,'1900-01-01 07:45:00.010'),2)
two digit year as primary key in sql server
One way of doing this, if you can alter the table structure, is to add a persisted computed column for the year part, and then add a primary key for (id, computer_col), like this:
CREATE TABLE myTable (
id INT NOT NULL,
d DATE NOT NULL,
y AS DATEPART(YEAR,d) PERSISTED NOT NULL,
PRIMARY KEY(id,y)
)
I'm not saying this is a good solution in any way, but it should work. Using a trigger on insert or a check constraint might be better.
Using your test data this will allow the first insert statement, but disallow the second as it violates the primary key constraint.
Related Topics
What Is and How to Remove Tablespace Error from My Database
Regex Pattern Inside SQL Replace Function
Sql71501 - How to Get Rid of This Error
Phpmyadmin Says No Privilege to Create Database, Despite Logged in as Root User
Mysql Workbench Edit Table Data Is Read Only
Ssis Date in Derived Column as Yyyy-Mm-Dd Format
How to Subtract One Day from Current Date Then Convert to String in Hive
Spring Data JPA Utf-8 Encoding Not Working
Pg_Restore: [Archiver] Unsupported Version (1.14) in File Header
Ssis Failed Validation and Returned Validation Status "Vs_Isbroken"
Using SQL to Determine Word Count Stats of a Text Field
How to Use Json_Extract in MySQL and Get a String Without the Quotes
How to Make an If Statement in Ssis
Run a Join Statement That Excludes Duplicate Rows
How to Pass Parameter to Mssql Query in Node Js
Removing Null Value in SQL Join and Union Operators in SQL
How to Get Calendar Quarter from a Date in Tsql
Fill a Field With the Values from Another Field in Ms Access